
1、配置文件配置扫描路径
<?xml version="1.0" encoding="UTF-8"?><beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"xmlns:context="http://www.springframework.org/schema/context"><bean class="com.alibaba.bean.Person" id="person" scope="singleton"><property name="age" value="18"></property><property name="name" value="张三"></property></bean><!--包扫描,只要标注:@Controller,@Service,@Repository,@Component都能扫到--><!--1、可以在配置文件配置扫描路径2、可以用注解@ComponentScan--><!-- <context:component-scan base-package="com.alibaba"></context:component-scan>--></beans>
2、注解@ComponentScan配置扫描路径,给配置类加注解
(1)value配置扫描的路径。
(2)可以用excludeFilters来排除不需要扫描的类。
(3)用includeFilters配置必须扫描的类,用此方法需要注意,useDefaultFilters默认是true,表示扫描配置路径下的所有包,所以需要 指定为false,此方法才可生效。
jdk1.8中 新增 Repeatable,ComponentScan是一个重复注解,可以用ComponentScans注解
* FilterType.ANNOTATION :注解名规则,比如Controller
* FilterType.ASSIGNABLE_TYPE : 按照给定的类型,比如当前类型的父子类。
* FilterType.ASPECTJ :使用aspectj表达式
* FilterType.REGEX :使用正则指定
* FilterType.CUSTOM :自定义Filter类代码如下。
先定义MyTypeFilter类实现TypeFilter,重写match方法,用metadataReader获取到定义路径中扫描的pojo,当返回true时候,才会放入容器,拿到这个pojo,若返回false,则拿不到这个pojo。我的代码里写的是过滤controller这个类,只有controller才会放入IOC容器。
/*** 扫描包自定义规则* metadataReader:读取到当前正在扫描类的信息* metadataReaderFactory:可以获取到其他任何类的信息** @author keying*/public class MyTypeFilter implements TypeFilter {public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException {//获取当前类注解信息AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();//获取当前正在扫描类的信息ClassMetadata classMetadata = metadataReader.getClassMetadata();//获取当前类资源信息 (比如类存在哪个盘,类的路径 )Resource resource = metadataReader.getResource();String name = classMetadata.getClassName();System.out.println("Filter扫描的类名:" + name);if(name.contains("controller")){return true;}return false;}}
重点,在ComponentScan注解里,定义@Filter,前面两个type为ANNOTATION,ASSIGNBALE_TYPE先注释掉,用FilterType.CUSTOM指定刚刚写的myTypeFilter类。
/*** 配置文件* @Configuration 告诉spring这是一个配置类** @ComponentScan 扫描路径配置* excludeFilters:指定排除扫描的包 ,按照特定的规则排除* includeFilters:指定要扫描的包 , useDefaultFilters:默认是true,默认扫描所有包,设置成false* jdk1.8中 新增 Repeatable,ComponentScan是一个重复注解,可以用ComponentScans注解* FilterType.ANNOTATION :注解名规则* FilterType.ASSIGNABLE_TYPE : 按照给定的类型* FilterType.ASPECTJ :使用aspectj表达式* FilterType.REGEX :使用正则指定* FilterType.CUSTOM :自定义** @author keying* @date 2021/6/24*/@Configuration@ComponentScans(value = {@ComponentScan(value = "com.alibaba", includeFilters = {*@Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),@Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {BookService.class})*/@Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})},useDefaultFilters = false)})public class BeanConfig {/*** @Bean吧对象注入给spring容器* 1、id默认是方法名,value方法可以指定方法名* @return Person*/@Bean(value = "person")public Person getPerson(){return new Person("李四",19);}}
最后在junit测试类,因为刚刚在Filter配置的只把controller放在IOC容器里,只能获取到Controller类。
/*** @ComponentScan*/@org.junit.Testpublic void test() {AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);String[] beanNames = annotationConfigApplicationContext.getBeanDefinitionNames();for (String beanName : beanNames) {System.out.println(beanName);}}
打印结果如下,只返回了controller,service和dao都没有返回:





