标签:tcl 开发 throw sele ada str 转换器 sed lse
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
(2)注解:带有@Configuration注解的类(配置类)
//配置类,相当于applicationContext.xml @Configuration public class MyConfig { @Bean //id=方法名(myStudent) public Student myStudent(){ Student student=new Student(2,"fg",34); return student; } }
取bean:
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class); Student myStudent = (Student) context.getBean("myStudent");
<context:component-scan base-package="org.ghl.controller"></context:component-scan>
排除: @ComponentScan(value = "org.ghl",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION ,value = {Service.class,Repository.class})}) 包含:(有默认行为,可以通过useDefaultFilters禁止) @ComponentScan(value = "org.ghl",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION ,value = {Service.class,Repository.class})},useDefaultFilters = false)
ASSIGNABLE_TYPE:指具体的类。
@ComponentScan(value = "org.ghl",includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE ,value = {StudentController.class})},useDefaultFilters = false)
@ComponentScan(value = "org.ghl",includeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM ,value = {MyFilter.class})},useDefaultFilters = false)
public class MyFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { ClassMetadata classMetadata = metadataReader.getClassMetadata(); //拿到扫描器value = "org.ghl"包中的所有标有三层组件注解类的名字 String className = classMetadata.getClassName(); //只过滤出和student相关的三层组件 if (className.contains("Student")){ return true; //表示包含 } return false; //表示排除 } }
public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { return new String[]{"org.ghl.entity.Apple","org.ghl.entity.Banana"}; //方法的返回值就是要纳入IOC容器的Bean } }
@Import({MyImportSelector.class})
(3)编写ImporBeanDefinitionRegistrar接口的实现类并重写方法。
public class MyImporBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { //BeanDefinition beanDefinition=new RootBeanDefinition(Orange.class); BeanDefinition beanDefinition=new RootBeanDefinition("org.ghl.entity.Orange"); beanDefinitionRegistry.registerBeanDefinition("myorange",beanDefinition); } }
@Import({MyImporBeanDefinitionRegistrar.class})
public class MyFactoryBean implements FactoryBean{ @Override public Object getObject() throws Exception { return new Apple(); } @Override public Class<?> getObjectType() { return Apple.class; } @Override public boolean isSingleton() { return true; } }
2.注册到@Bean中
@Bean public FactoryBean<Apple> myFactoryBean(){ return new MyFactoryBean(); }
注意:需要通过&区分获取的对象是哪一个。不加&,获取的是最内部真实的apple,如果加&,获取的是FactoryBean。
标签:tcl 开发 throw sele ada str 转换器 sed lse
原文地址:https://www.cnblogs.com/ghlz/p/13191225.html