2- @ComponentScan注解

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;
    }
}

总结

  1. excludeFilers = Filter[], 指定扫描的时候按照什么规则排除哪些组件
  2. includeFilers = Filter[], 指定扫描的时候按照什么规则包含哪些组件
  3. FilterType.ANNOTATION, 按照注解
  4. FilterType.ASSIGNABLE_TYPE,按照给定的类型
  5. FilterType.ASPECTJ,使用ASPECTJ表达式
  6. FilterType.REGEX,使用正则规则
  7. FilterType.CUSTOM,使用自定义规则
  8. @ComponentScan注解还可以叠加使用
发布了397 篇原创文章 · 获赞 71 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/xiaojie_570/article/details/104466320