文件结构: dao
@Repository public class PeopleDao { }Service
@Service public class DemoService { }@ComponentScan(basePackages = "com.example") basePackages 属性指定扫描路径
@Configuration @ComponentScan(basePackages = "com.example") public class Coinfig { }显示结果:
使用 includeFilters属性
@Configuration @ComponentScan(basePackages = "com.example",useDefaultFilters = false, includeFilters ={ @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Service.class) } ) public class Coinfig { }测试类
public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Coinfig.class); String[] beanDefinitionNames = context.getBeanDefinitionNames(); for (int i = 0; i <beanDefinitionNames.length ; i++) { System.out.println(beanDefinitionNames[i]); } } }显示结果:
使用 excludeFilters 属性
@Configuration @ComponentScan(basePackages = "com.example", excludeFilters ={ @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Service.class) } ) public class Coinfig { }type = FilterType.ANNOTATION属性默认,可以省略
