底层注解-@Import
@Import注解用于向容器中添加组件
@Import注解一定要使用在容器中的类上
- 什么叫容器中的类上?
- 就是该类以后会被加载到容器中, 比如配置类(@Configuration), 或者控制层类(@Controller)等
@Import注解源码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
/**
* {@link Configuration @Configuration}, {@link ImportSelector},
* {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
*/
Class<?>[] value();
}
可以看到@Import注解中有一个Class[], 这个数组其实就是我们要添加到容器中的组件的类的对象
比如有的时候我们某个类被加载到容器中的时, 如果此时需要加载其他一些类到容器中, 那么就可以使用@Import注解
@Import注解不仅仅是可以加载自定义类到容器中, 还可以加载第三方jar包中的类到容器中