Spring 7 -- developing spring based on annotation

Developing spring based on annotation There are two forms of spring IOC containers: (1) XML configuration file: applica...
Developing spring based on annotation There are two forms of spring IOC containers: (1) XML configuration file: applicationContext.xml ; Save bean: < bean > Take bean:
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

(2) Annotation: class with @ Configuration annotation (Configuration class)

Deposit bean:@Bean+ Return value of method
//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

(@ Scope("singleton")) scope="singleton": single example scope="prototype": prototype, multi instance. When to execute (when to generate bean s): singleton: when the container is initialized, it creates the object only once; it also supports delayed loading: when it is used for the first time, it creates the object. Add @ Lazy to config. prototype: when the container is initialized, it does not create objects, and creates objects every time it is used (every time it gets objects from the container). Condition note A Bean can be added to the IOC container under certain conditions. (1) Prepare bean s; (2) Add Condition beans: to set conditions for each bean, the Condition interface must be implemented. (3) Add IOC container according to conditions Review the ways to add beans to IOC: Note: all are set in @ Configuration configuration: Three layer component: Scanner + three layer annotation Non three level components: (1) @ Bean + return value (2)@import (3) FactoryBean @import uses: (1) write it directly into @ Import; @Import() (2) the implementation class of the custom ImportSelector interface is implemented through the selectimports method (the return value of the method is the Bean to be included in the IOC container). And tell the program to write its own implementation class.
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.

24 June 2020, 23:02 | Views: 7688

Add new comment

For adding a comment, please log in
or create account

0 comments