spring中注册bean到容器中的方法有很多,@Bean注解明显不适用于批量注册,所以spring中的@ComponentScan就是干这个事情的。
@ComponentScan
@ComponentScan用于批量注册bean。
这个注解会让spring去扫描某些包及其子包中所有的类,然后将满足一定条件的类作为bean注册到spring容器容器中。
通过源码查看注解的定义
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class) //@1
public @interface ComponentScan {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
String resourcePattern() default "**/*.class";
boolean useDefaultFilters() default true;
Filter[] includeFilters() default {};
Filter[] excludeFilters() default {};
boolean lazyInit() default false;
}
value:指定需要扫描的包,如:com.javacode2018
basePackages:作用同value;value和basePackages不能同时存在设置,可二选一
basePackageClasses:指定一些类,spring容器会扫描这些类所在的包及其子包中的类
nameGenerator:自定义bean名称生成器
resourcePattern:需要扫描包中的那些资源,默认是:**/*.class,即会扫描指定包中所有的class文件
useDefaultFilters:对扫描的类是否启用默认过滤器,默认为true
includeFilters:过滤器:用来配置被扫描出来的那些类会被作为组件注册到容器中
excludeFilters:过滤器,和includeFilters作用刚好相反,用来对扫描的类进行排除的,被排除的类不会被注册到容器中
lazyInit:是否延迟初始化被注册的bean
@1:@Repeatable(ComponentScans.class),这个注解可以同时使用多个。
在开发中会经常用到
@Component、@Repository、@Service、@Controller
来让系统更清晰,通常情况下,系统是分层结构的,将该类bean注册到spring容器中
查看注解代码发现,@Component、@Repository、@Service、@Controller、以及@Bean、@Configuration这些注解上都有@Comopnent
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
实际上通过注解将对象注册到容器中是@Comopnent这个注解,可以使用自定义注解继承@Comopnent也能实现对象注册到容器的效果
@ComponentScan各种用法。
注意类所在位置的关系
UserController
package com.example.demo.demo.annotationDemoController;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
}
Service
package com.example.demo.demo.annotationDemoService;
import org.springframework.stereotype.Service;
@Service
public class UserService {
}
UserDao
package com.example.demo.demo.annotationDemoDao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
}
UserModel在上面几个类的上一级包中,
package com.example.demo.demo;
import org.springframework.stereotype.Component;
@Component
public class UserModel {
}
扫描类UserComopentDemo
package com.example.demo.demo;
import org.springframework.context.annotation.ComponentScan;
//@ComponentScan
//@ComponentScan({"com.example.demo.demo.annotationDemoDao","com.example.demo.demo.annotationDemoService"})
@ComponentScan(basePackageClasses = UserModel.class)
public class UserComopentDemo {
}
单元测试
@Test
void componentTest(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserComopentDemo.class);
for (String beanName:
context.getBeanDefinitionNames()) {
System.out.println(beanName+"!!!"+context.getBean(beanName));
}
}
默认会扫描UserComopentDemo 类所在的包中的所有类包括子包下的类,类上有@Component、@Repository、@Service、@Controller任何一个注解(只要在注解的定义上有@Comopnent注解)的都会被注册到容器中,bean的命名是类首字母小写
configactionBeanDemo!!!com.example.demo.demo.ConfigactionBeanDemo$$EnhancerBySpringCGLIB$$2a89ab6@3fbfbf84
userModel!!!com.example.demo.demo.UserModel@23f72d88
userController!!!com.example.demo.demo.annotationDemoController.UserController@4bafe935
指定需要扫描
value:指定需要扫描的包,如:com.javacode2018
basePackages:作用同value;value和basePackages不能同时存在设置,可二选一
basePackageClasses:指定一些类,spring容器会扫描这些类所在的包及其子包中的类
//@ComponentScan
//@ComponentScan({"com.example.demo.demo.annotationDemoDao","com.example.demo.demo.annotationDemoService"})
@ComponentScan(basePackageClasses = UserModel.class)
指定包名的方式扫描存在的一个隐患,若包被重名了,会导致扫描会失效,一般情况下面使用basePackageClasses的方式来指定需要扫描的包,这个参数可以指定一些类型,默认会扫描这些类所在的包及其子包中所有的类
在@ComopnentScan注解中还有其他的一些参数
useDefaultFilters:对扫描的类是否启用默认过滤器,默认为true
includeFilters:过滤器:用来配置被扫描出来的那些类会被作为组件注册到容器中
excludeFilters:过滤器,和includeFilters作用刚好相反,用来对扫描的类进行排除的,被排除的类不会被注册到容器中
lazyInit:是否延迟初始化被注册的bean
例如
@ComponentScan(
useDefaultFilters = false, //不启用默认过滤器
includeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = IService.class) //@1
})
视实际情况使用
@ComponentScans
查看源码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ComponentScans {
ComponentScan[] value();
}
@ComponentScans注解用的比较少,作用跟@ComponentScan一样,只不过@ComponentScans可以包含多个@ComponentScan注解。
bean的批量注册补充@Import
一般了解到的批量注册
@Configuration结合@Bean注解的方式
@CompontentScan扫描包的方式
对于模块开发,使用的是调用的jar包的对象,这时候上面两种就不太适用了
@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();
}
看源码,参数为类的class
- 将@Import标注在类上,设置value参数
- 将@Import标注的类作为AnnotationConfigApplicationContext构造参数创建
AnnotationConfigApplicationContext对象 - 使用AnnotationConfigApplicationContext对象
@Import的value常见的有6种用法
- value为普通的类
- value为@Configuration标注的类
- value为@CompontentScan标注的类
- value为ImportBeanDefinitionRegistrar接口类型
- value为ImportSelector接口类型
- value为DeferredImportSelector接口类型
举一个例子
模块1配置类
/**
* 模块1配置类
*/
@Configuration
public class ConfigModule1 {
@Bean
public String module1() {
return "我是模块1配置类!";
}
}
模块2配置类
/**
* 模块2配置类
*/
@Configuration
public class ConfigModule2 {
@Bean
public String module2() {
return "我是模块2配置类!";
}
}
总配置类:通过@Import导入2个模块的配置类
/**
* 通过Import来汇总多个@Configuration标注的配置类
*/
@Import({ConfigModule1.class, ConfigModule2.class}) //@1
public class MainConfig2 {
}
测试方法
@Test
public void test2() {
//1.通过AnnotationConfigApplicationContext创建spring容器,参数为@Import标注的类
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
//2.输出容器中定义的所有bean信息
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(String.format("%s->%s", beanName, context.getBean(beanName)));
}
}
按模块的方式进行导入,需要哪个导入哪个,不需要的时候,直接修改一下总的配置类,调整一下@Import就可以了,非常方便。