- 1)包扫描+组件标注注解(
@Controller
/@Service
/@Repository
/@Component
)【局限于自己编写的组件】
- 2)
@Bean
【导入的第三方包中的组件】
- 3)
@Import
【快速给容器中导入一个组件】
- 1)
@Import(要导入到容器中的组件)
:容器中会自动注册这个组件,id 默认是全类名
- 2)
ImportSelector
:返回需要导入的组件的全类名数组
- 3)
ImportBeanDefinitionRegistrar
:手动注册 Bean 到容器中
Import 注解源码解析
1 2 3 4 5 6 7 8 9 10
| @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import {
Class<?>[] value(); }
|
@Import
只能作用于类上,可以传入“一组” Class 类
- 可以是需要导入到容器中的组件
Bean
- 可以是
ImportSelector
实现类
- 可以是
ImportBeanDefinitionRegistrar
实现类
ImportSelector 接口源码解析
1 2 3
| public interface ImportSelector { String[] selectImports(AnnotationMetadata importingClassMetadata); }
|
其中定义一个 selectImports
方法,提供了一个入参
1 2 3 4 5 6 7 8 9 10 11 12 13
| public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata { Set<String> getAnnotationTypes();
Set<String> getMetaAnnotationTypes(String annotationName);
boolean hasAnnotation(String annotationName);
boolean hasMetaAnnotation(String metaAnnotationName);
boolean hasAnnotatedMethods(String annotationName);
Set<MethodMetadata> getAnnotatedMethods(String annotationName); }
|
ImportBeanDefinitionRegistrar 接口源码解析
1 2 3 4 5
| public interface ImportBeanDefinitionRegistrar { public void registerBeanDefinitions( AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
}
|
其中定义一个 registerBeanDefinitions
方法,提供了两个入参
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public interface BeanDefinitionRegistry extends AliasRegistry { void registerBeanDefinition(String var1, BeanDefinition var2) throws BeanDefinitionStoreException;
void removeBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
boolean containsBeanDefinition(String var1);
String[] getBeanDefinitionNames();
int getBeanDefinitionCount();
boolean isBeanNameInUse(String var1); }
|
测试
实体类
1 2 3 4 5
| public class Red {} public class Yellow {} public class Green {} public class Blue {} public class Rainbow {}
|
配置类
1 2 3
| @Configuration @Import({Red.class, Yellow.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class}) public class SpringConfig4 {}
|
自定义导入选择器
1 2 3 4 5 6 7 8
| public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[] {Green.class.getName(), Blue.class.getName()}; } }
|
自定义导入 Bean 定义注册器
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean beanDefinition1 = registry.containsBeanDefinition(Red.class.getName()); boolean beanDefinition2 = registry.containsBeanDefinition(Green.class.getName()); boolean beanDefinition3 = registry.containsBeanDefinition(Blue.class.getName()); if (beanDefinition1 && beanDefinition2 && beanDefinition3) { BeanDefinition beanDefinition = new RootBeanDefinition(Rainbow.class); registry.registerBeanDefinition("rainbow", beanDefinition); } } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class SpringAnnotationTest4 { private ApplicationContext context;
@Before public void initContext() { context = new AnnotationConfigApplicationContext(SpringConfig4.class); }
@Test public void testImport() { String[] names = context.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } System.out.println("==============="); Red red = context.getBean(Red.class.getName(), Red.class); Yellow yellow = context.getBean(Yellow.class.getName(), Yellow.class); Green green = context.getBean(Green.class.getName(), Green.class); Blue blue = context.getBean(Blue.class.getName(), Blue.class); Rainbow rainbow = context.getBean("rainbow", Rainbow.class); System.out.println(red); System.out.println(yellow); System.out.println(green); System.out.println(blue); System.out.println(rainbow); } }
|
测试结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory springConfig4 com.vectorx.springannotation.entity.Red com.vectorx.springannotation.entity.Yellow com.vectorx.springannotation.entity.Green com.vectorx.springannotation.entity.Blue rainbow =============== com.vectorx.springannotation.entity.Red@ca263c2 com.vectorx.springannotation.entity.Yellow@589b3632 com.vectorx.springannotation.entity.Green@45f45fa1 com.vectorx.springannotation.entity.Blue@4c6e276e com.vectorx.springannotation.entity.Rainbow@534df152
|