Spring注解编程

@Configuration和@ComponentScan注解

2018-03-12  本文已影响0人  大雄_bafc

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用法结果
上一篇下一篇

猜你喜欢

热点阅读