在容器中读取Bean的信息,如遇到对扫包做
public class MainClass { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class); String[] beanDefinationNames = ctx.getBeanDefinitionNames(); for (String name:beanDefinationNames) { System.out.println("bean的定义信息:"+name); } } }
在配置类上通过@CompentScan注解来进行包扫描
@Configuration @ComponentScan(basePackages = {"com.test.testcompentscan"}) public class MainConfig { }
1、排除用法 excludeFilters(排除@Controller注解的,和testService的)
@ComponentScan(basePackages = {"com.tuling.testcompentscan"},excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class}), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = {TulingService.class}) })
2、包含用法includeFilters,需要注意若使用包含用法,需要把useDefaultFilters属性设置为false,默认true代表扫全部。排除掉包含Controller、Service
@Configuration
@ComponentScan(basePackages = {"com.tuling.testcompentscan"},includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, Service.class})
},useDefaultFilters = false)
public class MainConfig {
}
3、自定义过滤类型
@ComponentScan(basePackages = {"com.tuling.testcompentscan"},includeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM,value = TestFilterType.class) },useDefaultFilters = false)
public class TestFilterType implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //获取当前类的注解源信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //获取当前类的class的源信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); //获取当前类的资源信息 Resource resource = metadataReader.getResource(); System.out.println("类的路径:"+classMetadata.getClassName()); if(classMetadata.getClassName().contains("dao")) { return true; } return false; } }
组合使用,根据自定义类型排除bean,同时使用包含排除掉Repository的bean
@ComponentScan(basePackages = {"com.test.testcompentscan"},excludeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM,value = TestFilterType.class) },includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Repository.class) })
@ComponentScan.Filter注解的 tyoe类型
FilterType.ANNOTATION 表示 @Controller @Service @Repository @Compent
FilterType.ASSIGNABLE_TYPE 指定类型
FilterType.CUSTOM 自定义类型 @ComponentScan.Filter(type =FilterType.ASSIGNABLE_TYPE,value = {TestService.class}) /*TestService 继承 TypeFilter 自定义过滤*/
通过@import注解往容器添加组件 当需要使用到第三方组件的时候使用 ,新建Person,Car ,ImportSelector
public class MainClass { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class); for(String beanName:ctx.getBeanDefinitionNames()) { System.out.println("beanName:"+beanName); } } }
实现接口selectImports,重写方法selectImports会要求返回字符串的数组,这个数组中放需要加载到容器中的类
/*导入类 会将Person,Car,ImportSelector 加载到容器中*/ @Import(value = {Person.class, Car.class,ImportSelector.class}) public class MainConfig { } public class ImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"com.tuling.testimport.compent.Dog"}; } }
运行打印
beanName:mainConfig
beanName:com.tuling.testimport.compent.Person
beanName:com.tuling.testimport.compent.Car
beanName:com.tuling.testimport.compent.Dog
通过@Import 使用ImportBeanDefinitionRegistrar类往容器中添加组件
@Import(value = {Person.class, Car.class, TestImportSelector.class, TestBeanDefinitionRegister.class})
public class MainConfig {
}
public class TestBeanDefinitionRegister implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Cat.class); registry.registerBeanDefinition("cat",rootBeanDefinition); } }