@Configuration和@ComponentScan注解

Spring注解

一、组建注册

包结构:

包结构

1.@Configuration和@ComponentScan

includeFilters用法

//相当于application-bean.xml
@Configuration 
//配置扫描包的策略,当前注解为只扫描加@Controller的组件
//注意useDefaultFilters要设置为fasle,不然为默认的Filters,@Service/@Repository都扫描
@ComponentScan(value = "com.gaoyuzhe",includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)
},
        useDefaultFilters = false)
public class MainConfig {

    @Bean
    public Person person(){
        return new Person("gaoyuzhe ",12);
    }
}

运行结果

includeFilters结果

excludeFilters用法:

注意:useDefaultFilters设置为true,不然不会扫描到组件

/**
 * @author GaoYuzhe
 * @date 2018/3/12.
 */
@Configuration
@ComponentScan(value = "com.gaoyuzhe",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)
},
        useDefaultFilters = true)
public class MainConfig {

    @Bean
    public Person person(){
        return new Person("gaoyuzhe ",12);
    }
}

运行结果

excludeFilters用法结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,891评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,947评论 6 342
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,721评论 0 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,165评论 25 708
  • 开车路上听之前在得到app买的精品专栏,莫名其妙的突然想听武志红的,然后就点开了 他说的是“如何认清你和身边人的关...
    蜕变的林林阅读 602评论 0 0