Spring常用注解

@Configuration通常用在配置类上,告诉spring这是一个配置类(配置类类似配置文件,区别在于用类的形式来表现xml;

@Service用于标注业务层组件service层,

@Controller用于标注控制层组件(如struts中的action) ,

@Repository用于标注数据访问组件,即DAO组件,

@component把普通pojo实例化到spring容器中,相当于配置文件中的 <bean id="" class=""/>

使用@Configuration会扫描以上四种注解声明的类。

@Bean在配置类中使用时,表示这是一个JavaBean。

//配置类 == 配置文件
@ComponentScan(value = "com.spring") // 告诉spring这个是一个配置类
})
public class MainConfig {
 
    // 注册一个bean,类型为返回值的类型,id默认用方法名
    @Bean
    public Person person1() {
        return new Person("张三", 20);
    }
 
    //可以为bean定义id
    @Bean("student")
    public Person person2() {
        return new Person("韩梅梅", 12);
    }
 
}

@ComponentSan value:指定要扫描的包
excludeFilters = Filter[] :指定扫描的时候按照什么规则排除哪些组件
includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
FilterType.ANNOTATION:注解类型
FilterType.ASSIGNABLE_TYPE:按照指定的类型,如指定BookService,会查找BookService本身,及其子类
FilterType.ASPECTJ:使用ASPECTJ表达式,不经常使用
FilterType.REGEX:使用正则表达式
FilterType.CUSTOM:使用自定义表达式

@ComponentScan排除扫描类型

@ComponentScan(value = "com.spring", excludeFilters = {//排除指定的扫描类型
        //过滤注解类型   @Controller @Repository
        @Filter(type = FilterType.ANNOTATION, classes = { Controller.class,Repository.class }) 
})

@ComponentScan扫描指定类型

@ComponentScan(value = "com.spring", includeFilters = {//指定的扫描类型
        //过滤注解类型   @Controller @Repository
        @Filter(type = FilterType.ANNOTATION, classes = { Controller.class,Repository.class }) 
},useDefaultFilters = false)//注意,使用扫描指定类型时,需要配置useDefaultFilters = false  禁用默认规则

@ComponentScans可以理解为@ComponentScan数组,可以配置多个@ComponentScan

@ComponentScans(value= {
        @ComponentScan(value = "com.spring", includeFilters = {//指定的扫描类型
                //过滤注解类型   @Controller @Repository
                @Filter(type = FilterType.ANNOTATION, classes = { Controller.class,Repository.class }) ,
                                //指定类型
                @Filter(type = FilterType.ASSIGNABLE_TYPE,classes = (BookService.class)),
                                //自定义
                @Filter(type = FilterType.CUSTOM,classes = (MyTypeFilter.class))
        },useDefaultFilters = false)//注意,使用扫描指定类型时,需要配置useDefaultFilters = false  禁用默认规则
})

自定义包含规则

public class MyTypeFilter implements TypeFilter {
 
    /**
      * 返回true匹配成功,返回false匹配失败 
     * metadataReader 读取到的当前正在扫描的类的信息
     * metadataReaderFactory 可以获取到其他任何类的信息
     */
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
            throws IOException {
        //获取当前类注解的信息
        AnnotationMetadata nnnotationmetadata = 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;
    }
 
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容