1. @ComponentScan
注解
在配置类中只要标注了@ComponentScan
注解,Spring就可以自动扫描Value对应的包了
1.1. 配置类
package com.spring.config;
import com.spring.beans.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = "com.spring")
public class MainConfig {
@Bean
public Person person() {
return new Person("fj",26);
}
}
以上扫描com.spring包中的所有组件
1.2 单元测试类
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class IOCTest_1 {
@SuppressWarnings("resource")
@Test
public void test01() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
// 获取容器中的所有组件
String[] names = applicationContext.getBeanDefinitionNames();
for(String name: names)
System.out.println(name);
}
}
输出的结果
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
bookController
bookService
bookDao
person
前面五个都是Spring自己的组件,其中mainConfig
类是因为增加了@Configuration
注解,bookController
类是因为增加了@Controller
注解,bookService
类是因为增加了@Servide
注解,bookDao
类是因为增加了@Repository
注解,pereson
类是因为在mainConfig
类的person方法中增加了@Bean
注解
1.3 excludeFilters
排除
package com.spring.config;
import com.spring.beans.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(value = "com.spring", excludeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class})
})
public class MainConfig {
@Bean
public Person person() {
return new Person("fj",26);
}
}
根据注解类型进行排除
1.4 includeFilters
仅包含
package com.spring.config;
import com.spring.beans.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(value = "com.spring", includeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
public class MainConfig {
@Bean
public Person person() {
return new Person("fj",26);
}
}
使用仅包含includeFilters
需要禁用默认的过滤规则,即将useDefaultFilters
设置为false
1.5 FilterType.CUSTOM
自定义过滤规则
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import java.io.IOException;
public class MyTypeFilter implements TypeFilter {
@Override
// MetadataReader: 读取到的当前正在扫描的类的信息
// MetadataReaderFactory: 可以获取到其他任何类的信息
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
// 获取当前类注解的信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
// 获取到当前正在扫描的类的类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 获取当前类资源(类的路径)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
System.out.println("类名:" + className);
if(className.contains("dao"))
return true;
return false;
}
}
总结
excludeFilers = Filter[]
, 指定扫描的时候按照什么规则排除哪些组件includeFilers = Filter[]
, 指定扫描的时候按照什么规则包含哪些组件FilterType.ANNOTATION
, 按照注解FilterType.ASSIGNABLE_TYPE
,按照给定的类型FilterType.ASPECTJ
,使用ASPECTJ表达式FilterType.REGEX
,使用正则规则FilterType.CUSTOM
,使用自定义规则@ComponentScan
注解还可以叠加使用