Spring注解驱动开发——组件注册 @ComponentScans + 自定义TypeFilter指定过滤规则

1 @ComponentScans

 @ComponentScans 相当于多个 @ComponentScan 的集合  可以多指定扫描规则

2 FilterType

一 丶FilterType.ANNOTATION:按照注解规则

 a. 在conf 配置类下指定扫描规则

只有@Controller @Service 标注的类, 注入到ioc容器中 @Repository 不注入。

package com.mongoubiubiu.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.mongoubiubiu.bean.Black;
import com.mongoubiubiu.bean.ColorFactoryBean;
import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.bean.Red;
import com.mongoubiubiu.bean.Yellow;
import com.mongoubiubiu.condition.MyInportSelect;
import com.mongoubiubiu.dao.UserDao;
import com.mongoubiubiu.service.UserService;

//配置类==配置文件
@Configuration  //告诉spring 这是一个配置类

@ComponentScans(value={
@ComponentScan(value="com.mongoubiubiu",includeFilters={
                //type=FilterType.ANNOTATION 要注入的类型   classes=Controller.class 要注入哪个注解
                //除了controller 标注的 其他都注入到容器中
                @Filter(type=FilterType.ANNOTATION,classes={Controller.class,Service.class})    
                
        },useDefaultFilters=false)
})
public class MyConf {
    
    //给容器中注册一个bean; 类型为返回值的类型 id默认是用方法名做为id
    @Bean("xixi")
    public Person person222(){
        return new Person("王五", 19);
    }
    
    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }
    
}

 b. 测试

二 丶FilterType.ASSIGNABLE_TYPE:按照给定的类型;

a. 在conf 配置类下指定扫描规则

只有UserService.class 的类型才能被注入。

package com.mongoubiubiu.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.mongoubiubiu.bean.Black;
import com.mongoubiubiu.bean.ColorFactoryBean;
import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.bean.Red;
import com.mongoubiubiu.bean.Yellow;
import com.mongoubiubiu.condition.MyInportSelect;
import com.mongoubiubiu.dao.UserDao;
import com.mongoubiubiu.service.UserService;

//配置类==配置文件
@Configuration  //告诉spring 这是一个配置类

@ComponentScans(value={
        @ComponentScan(value="com.mongoubiubiu",includeFilters={
@Filter(type
=FilterType.ASSIGNABLE_TYPE,classes={UserService.class}) },useDefaultFilters=false) }) public class MyConf { //给容器中注册一个bean; 类型为返回值的类型 id默认是用方法名做为id @Bean("xixi") public Person person222(){ return new Person("王五", 19); } @Bean public ColorFactoryBean colorFactoryBean(){ return new ColorFactoryBean(); } }

 b. 测试

三 丶FilterType.CUSTOM:使用自定义规则

a.自定义规则  名字包含er的都被注入进来

package com.mongoubiubiu.conf;

import java.io.IOException;

import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

public class MyTypeFilter implements TypeFilter {

    /**
     * MetadataReader: 读取到的当前正在扫描类的信息
     * 
     * MetadataReaderFactory:可以获取到其他任何类的信息
     * 
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {


        //获取当前类注解的信息
        AnnotationMetadata annotationMetadata=    metadataReader.getAnnotationMetadata();
        
        //获取当前类的资源信息(类的路径)
        Resource resource=metadataReader.getResource();
        //获取当前扫描类的全类名
        String className=annotationMetadata.getClassName();
        System.out.println("=======>>>"+className);
        
        
        if(className.contains("er")){
            return true;
        }

        return false;
    }

}

b. 在conf 配置类下指定扫描规则

package com.mongoubiubiu.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.mongoubiubiu.bean.Black;
import com.mongoubiubiu.bean.ColorFactoryBean;
import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.bean.Red;
import com.mongoubiubiu.bean.Yellow;
import com.mongoubiubiu.condition.MyInportSelect;
import com.mongoubiubiu.dao.UserDao;
import com.mongoubiubiu.service.UserService;

//配置类==配置文件
@Configuration  //告诉spring 这是一个配置类
@ComponentScans(value={
        @ComponentScan(value="com.mongoubiubiu",includeFilters={    
                @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})    
        },useDefaultFilters=false)
})
public class MyConf {
    
    //给容器中注册一个bean; 类型为返回值的类型 id默认是用方法名做为id
    @Bean("xixi")
    public Person person222(){
        return new Person("王五", 19);
    }
    
    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }
    
}

c.测试 

 

posted @ 2021-07-14 23:36  KwFruit  阅读(103)  评论(0编辑  收藏  举报