Spring7——开发基于注解形式的spring
2021-05-06 01:27
标签:tcl 开发 throw sele ada str 转换器 sed lse (2)注解:带有@Configuration注解的类(配置类) 取bean: ASSIGNABLE_TYPE:指具体的类。 (3)编写ImporBeanDefinitionRegistrar接口的实现类并重写方法。 2.注册到@Bean中 注意:需要通过&区分获取的对象是哪一个。不加&,获取的是最内部真实的apple,如果加&,获取的是FactoryBean。 Spring7——开发基于注解形式的spring 标签:tcl 开发 throw sele ada str 转换器 sed lse 原文地址:https://www.cnblogs.com/ghlz/p/13191225.htmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//配置类,相当于applicationContext.xml
@Configuration
public class MyConfig {
@Bean //id=方法名(myStudent)
public Student myStudent(){
Student student=new Student(2,"fg",34);
return student;
}
}
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class);
Student myStudent = (Student) context.getBean("myStudent");
排除:
@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)
@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})
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;
}
}
@Bean
public FactoryBean myFactoryBean(){
return new MyFactoryBean();
}
文章标题:Spring7——开发基于注解形式的spring
文章链接:http://soscw.com/index.php/essay/82983.html