Spring注解开发—— 4、组件注册-自定义TypeFilter指定过滤规则 4、组件注册-自定义TypeFilter指定过滤规则

4.1 FilterType.ANNOTATION 按照注解方式

package com.suirui.springanno.config;

import com.suirui.springanno.entity.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Configuration  //告诉Spring这是一个配置类
@ComponentScan(value = "com.suirui", useDefaultFilters = false,
        includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),//按照注解进行过滤
//                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}),
//                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
        }
)
public class MainConfig {

    @Bean("personAnno") //给容器注册一个bean,类型为返回值类型,id默认是方法名,可以通过注解value替换名称
    public Person person() {
       return  new Person("zx",28);
    }

}

package com.suirui.springanno;


import com.suirui.springanno.config.MainConfig;
import com.suirui.springanno.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig.class);
        //获取所有的BeanDefinitionNames
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
    }
}

4.2 FilterType.ASSIGNABLE_TYPE 按照给定的类型

package com.suirui.springanno.config;

import com.suirui.springanno.Service.BookService;
import com.suirui.springanno.entity.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Configuration  //告诉Spring这是一个配置类
@ComponentScan(value = "com.suirui", useDefaultFilters = false,
        includeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),//按照注解进行过滤
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}),//按照给定类型过滤
//                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
        }
)
public class MainConfig {

    @Bean("personAnno") //给容器注册一个bean,类型为返回值类型,id默认是方法名,可以通过注解value替换名称
    public Person person() {
       return  new Person("zx",28);
    }

}
 

4.3 FilterType.ASPECTJ 按照ASPECTJ表达式,不常用

4.4 FilterType.REGEX 按照正则表达

4.5 FilterType.CUSTOM 按照自定义规则

package com.suirui.springanno.config;

import com.suirui.springanno.Service.BookService;
import com.suirui.springanno.entity.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Configuration  //告诉Spring这是一个配置类
@ComponentScan(value = "com.suirui", useDefaultFilters = false,
        includeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),//按照注解进行过滤
//        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}),//按照给定类型过滤
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
        }
)
public class MainConfig {

    @Bean("personAnno") //给容器注册一个bean,类型为返回值类型,id默认是方法名,可以通过注解value替换名称
    public Person person() {
       return  new Person("zx",28);
    }

}


package com.suirui.springanno.config;

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 {
    /**
     * Determine whether this filter matches for the class described by
     * the given metadata.
     *
     * @param metadataReader        the metadata reader for the target class    读取到得当前正在扫描的类的信息
     * @param metadataReaderFactory a factory for obtaining metadata readers    一个可以探索其他类信息的工厂类
     *                              for other classes (such as superclasses and interfaces)
     * @return whether this filter matches
     * @throws IOException in case of I/O failure when reading metadata
     */
    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("er")) {
            return true;
        }
        return false;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值