package com.lei.study_09_13.config; import com.lei.study_09_13.bean.Person; import org.springframework.context.annotation.*; import org.springframework.stereotype.Controller; /** * * * @author LeiLei * @date 2019/9/13 */ @Configuration //该注解可扫描到@Controller、@Service、@Repository、@Component注解的组件 //该注解内部的@Repeatable(ComponentScans.class)注解表明重复使用该注解使用ComponentScans @ComponentScan("com.lei.study_09_13.web") //排除掉controller注解的组件 /*@ComponentScan(basePackages = "com.lei.study_09_13.web",excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) })*/ //只装配Controller注解的组件,注意useDefaultFilters如果不设置为false,则不生效 /*@ComponentScan(basePackages = "com.lei.study_09_13.web",includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) },useDefaultFilters = false)*/ //多个ComponentScan一起使用 /*@ComponentScans(value = { @ComponentScan(basePackages = "com.lei.study_09_13.web",includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) },useDefaultFilters = false) })*/ public class BeanConfig { @Bean public Person person() { return new Person("lei",24); } @Bean public Person getPerson() { return new Person("lei",25); } @Bean("person2") public Person person2() { return new Person("lei2",26); } }
总结:
1.该注解可扫描到@Controller、@Service、@Repository、@Component注解的组件
2.可设定include、exclude过滤,使用include过滤时注意将useDefaultFilters设置为false
3.使用多个ComponentScan时使用ComponentScans注解