1. Preparation
1. Import spring-context dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
2. Create entity classes Red, Blue, Yellow
public class Red {
}
public class Blue {
}
public class Yellow {
}
Second, the use of annotations
1. Create a BeanConfig configuration class
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//@Import(Red.class)
@Import({Red.class, Yellow.class, Blue.class})
@Configuration
public class BeansConfig {
}
2. Start the test
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
String[] names = context.getBeanDefinitionNames();
for (String name:names) {
System.out.println(name);
}
/**
* @import注入bean的名称为:全类名
* 包路径.Red
* 包路径.Yellow
* 包路径.Blue
*/
}
三、ImportSelector
1. Create ColorSelector
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class ColorSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 这里可以通过AnnotationMetadata处理业务逻辑,为了演示我们就尽量简单着来了
return new String[]{Red.class.getName(), Blue.class.getName(), Yellow.class.getName()};
}
}
2. Configure to @Import
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//@Import({Red.class, Yellow.class, Blue.class})
@Import(ColorSelector.class)
@Configuration
public class BeansConfig {
}
3. Re-test, the result is the same
四、ImportBeanDefinitionRegistrar
1. Create a Color Registrar
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class ColorRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// 如果red没有注入,则手动注入
if(!registry.containsBeanDefinition(Red.class.getName())){
RootBeanDefinition red = new RootBeanDefinition(Red.class);
registry.registerBeanDefinition("red",red);
}
// 如果blue没有注入,则手动注入
if(!registry.containsBeanDefinition(Blue.class.getName())){
RootBeanDefinition blue = new RootBeanDefinition(Blue.class);
registry.registerBeanDefinition("blue",blue);
}
// 如果yellow没有注入,则手动注入
if(!registry.containsBeanDefinition(Yellow.class.getName())){
RootBeanDefinition yellow = new RootBeanDefinition(Yellow.class);
registry.registerBeanDefinition("yellow",yellow);
}
}
}
2. Configure to @Import
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//@Import(ColorSelector.class)
@Import(ColorRegistrar.class)
@Configuration
public class BeansConfig {
}
3. Test again, the result is the same
3. Summary and analysis
This chapter mainly learns three ways to inject Bean using @Import annotation. "Reference Materials"