Article Directory
- 1. Component annotation
- 2、@Component + @Bean
- 3. @Import(PlaceHolderClass) Quickly import a component
- 4. Use FactoryBean Injection provided by Spring
1. Component annotation
annotation | describe |
---|---|
@Component |
Annotations when component definition is not clear |
@Controller |
Controller layer |
@Service |
Service layer |
@Repository |
Data layer |
Note: The class that adds annotations needs to be in the same package path as the startup class. If it is in another package, it needs to be inStartup class
orThe class with the above annotation under the package where the startup class is located
Added in@ComponentScan
annotation.
For example:
Startup class:
package com.springboottest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"", ""})
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
Student class (not in the same package path as the startup class):
package com.springtest;
import lombok.Data;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
@Data
@Component
public class Student {
private String name;
private String nickName;
}
2、@Component + @Bean
@Component
public class Test {
@Bean
public User user() {
return new User();
}
}
Note:@Bean
Annotation method, return valueUser
As injectedBean
Object.
3. @Import(PlaceHolderClass) Quickly import a component
PlaceHolderClass | describe |
---|---|
General Class | Direct injection |
Class that implements the ImportSelector interface | Injection according to the fully qualified array of the return class |
Class that implements ImportBeanDefinitionRegistrar interface | Get class description information, selective manual injection |
@Configuration
@Import({ImportDemo.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class ImportConfig{
@Bean
public User user(){
return new User();
}
}
//Custom logic returns the components that need to be imported
public class MyImportSelector implements ImportSelector {
//The return value is an array of fully qualified names of components imported into the container
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//All annotations of the current class
Set<String> annotationTypes = importingClassMetadata.getAnnotationTypes();
System.out.println("Annotation information of the current configuration class:"+annotationTypes);
return new String[]{".user01",".user02"};
}
}
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
* AnnotationMetadata: The annotation information of the current class
* BeanDefinitionRegistry:BeanDefinition registration class;
* Add all beans that need to be added to the container; call
* Manual registration
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean definition = registry.containsBeanDefinition(".User01");
boolean definition2 = registry.containsBeanDefinition(".User02");
if(definition && definition2){
//Create a BeanDefinition (Bean's description information object)
RootBeanDefinition beanDefinition = new RootBeanDefinition(User03.class);
//Register a bean and specify the bean name
registry.registerBeanDefinition("User03", beanDefinition);
}
}
}
4. Use FactoryBean Injection provided by Spring
public class UserFactoryBean implements FactoryBean<User> {
// Injected Bean
@Override
public User getObject() throws Exception {
return new User();
}
// Injected Bean type
@Override
public Class<?> getObjectType() {
return User.class;
}
// Is it a single case
@Override
public boolean isSingleton() {
return true;
}
}