美文网首页Spring注解编程
@Configuration和@ComponentScan注解

@Configuration和@ComponentScan注解

作者: 大雄_bafc | 来源:发表于2018-03-12 16:13 被阅读0次

    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用法结果

    相关文章

      网友评论

        本文标题:@Configuration和@ComponentScan注解

        本文链接:https://www.haomeiwen.com/subject/soonfftx.html