ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
(2) Annotation: class with @ Configuration annotation (Configuration class)
//Configuration class, equivalent to applicationContext.xml @Configuration public class MyConfig { @Bean //id = method name (myStudent) public Student myStudent(){ Student student=new Student(2,"fg",34); return student; } }
Take bean:
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class); Student myStudent = (Student) context.getBean("myStudent");Note: IOC obtained in two forms is independent Detailed explanation of annotation form to IOC container storage bean: 1. There must be @ Configuration 2. Form: 2.1 three layer components (Controller, Service, Dao): (1) annotate the three-tier components with @ Controller, @ Service, @ Repository equivalent to @ comment (2) included scanner a.xml configuration
<context:component-scan base-package="org.ghl.controller"></context:component-scan>b. Annotation form Component scan is only responsible for three-tier components. To assign rules to a scanner: Filter type: FilterType(ANNOTATION, ASSIGNABLE_TYPE, CUSTOM) ANNOTATION: three level ANNOTATION types @ Controller, @ Service, @ Repository are equivalent to @ comment exclude: @ComponentScan(value = "org.ghl",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION ,value = )}) Include: (there is default behavior, which can be prohibited through useDefaultFilters) @ComponentScan(value = "org.ghl",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION ,value = )},useDefaultFilters = false)
ASSIGNABLE_TYPE: refers to a specific class.
@ComponentScan(value = "org.ghl",includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE ,value = )},useDefaultFilters = false)Distinction: ANNOTATION: Service.class Indicators have all classes of @ service; ASSIGNABLE_TYPE: refers to a specific class. CUSTOM: CUSTOM: define rules by yourself
@ComponentScan(value = "org.ghl",includeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM ,value = )},useDefaultFilters = false)
public class MyFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { ClassMetadata classMetadata = metadataReader.getClassMetadata(); //Get scanner value =“ org.ghl "All of the packages are labeled with the names of the three-tier Component annotation classes String className = classMetadata.getClassName(); //Filter out only the three-tier components related to student s if (className.contains("Student")){ return true; //Indicates contains } return false; //Indicates exclusion } }
2.2 non three layer components( Student.clss/ Converter, etc.): (1) The return value of @ Bean + method. The default value of id is method name, which can also be modified through @ Bean("stu"). (2)import/FactoryBean Scope of bean
public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { return new String[]{"org.ghl.entity.Apple","org.ghl.entity.Banana"}; //The return value of the method is the Bean to be included in the IOC container } }
@Import()
(3) write the implementation class of the importbeandefinitionregister interface and override the method.
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()
FactoryBean 1. Write implementation class and rewrite method
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. Register in @ Bean
@Bean public FactoryBean<Apple> myFactoryBean(){ return new MyFactoryBean(); }
Note: it is necessary to distinguish which object is obtained through. Without &, the most internal real apple is obtained. If &, the FactoryBean is obtained.