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注解还可以叠加使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值