Spring注解02 @ComponentScan 自动扫描组件
前言
现今SpringBoot、SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解、原理,比如@Conditional、@Import、@EnableXXX等。
如果掌握这些底层原理、注解,那么我们对这些高层框架就能做到高度定制,使用的游刃有余;
本文涵盖Spring的常用注解
@ComponentScan、@Bean、@Configuration、@Conditional、@Import、@PropertySource、@Profile等
1.@Configuration
- 1.1 配置类
@Configuration //告诉Spring这是一个配置类
public class MainConfig {
//给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
@Bean(value = "person01")
public Person person(){
return new Person("jm",16,"tommy");
}
}
- 1.2 测试类
public class MainTest {
public static void main(String[] args) {
final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
final Person person = applicationContext.getBean(Person.class);
System.out.println("person = [" + person + "]");
final String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : namesForType) {
System.out.println(name);
}
}
}
- 1.3 运行结果
person = [Person(name=jm, age=16, nickName=tommy)]
person01
Process finished with exit code 0
- 1.4 结果分析
person01
给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id,也可以指定
2 @ComponentScans @ComponentScan
包扫描、只要标注了@Controller、@Service、@Repository,@Component
- 2.1 配置文件方式:
<context:component-scan base-package="com.tommy" use-default-filters="false"></context:component-scan>
- 2.2 @ComponentScans方式
- 2.2.1 配置类
@ComponentScan value:指定要扫描的包
@Configuration
@ComponentScan(value = "com.tommy")
public class MainConfig {
//给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
@Bean(value = "person")
public Person person01() {
return new Person("jm", 16, "tommy");
}
}
运行结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
personController
personDao
personService
person
- 2.2.2 excludeFilters
excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
@Configuration
@ComponentScan(value = "com.tommy",
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = {Controller.class, Service.class})
})
运行结果:
....省略....
mainConfig
personDao
person
- 2.2.3 includeFilters
includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
@Configuration
@ComponentScan(value = "com.tommy",
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = {Controller.class, Service.class})
},useDefaultFilters = false )
运行结果:
.....省略.....
personController
personService
person
*注意:使用includeFilters 时,必须设置 useDefaultFilters = false,才能生效。
- 2.2.4 @ComponentScans
@Configuration
@ComponentScans(value = {
@ComponentScan(value = "com.tommy",
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = {Controller.class, Service.class})
}, useDefaultFilters = false)
})
- 2.2.5 FilterType
FilterType.ANNOTATION:按照注解
FilterType.ASSIGNABLE_TYPE:按照给定的类型;
FilterType.ASPECTJ:使用ASPECTJ表达式
FilterType.REGEX:使用正则指定
FilterType.CUSTOM:使用自定义规则
- 2.2.5.1 CUSTOM自定义规则
配置类
@ComponentScan(value = "com.tommy",
includeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
}, useDefaultFilters = false)
MyTypeFilter .java
public class MyTypeFilter implements TypeFilter {
/**
* @param metadataReader 读取到的当前正在扫描的类的信息
* @param metadataReaderFactory 可以获取到其他任何类信息的
* @return
* @throws IOException
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
final AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
final ClassMetadata classMetadata = metadataReader.getClassMetadata();
final Resource resource = metadataReader.getResource();
final String className = classMetadata.getClassName();
if (className.contains("er")) {
System.out.println("--->" + className);
return true;
}
return false;
}
}