Spring multi-sample, BeanFactoryAware, ApplicationContextAware, InitializingBean Interface
In web project development, the following interfaces are applied if you need to customize the IOC process.
Dynamic proxy InitializingBean Interface
Reference
1: spring provides two bean initialization methods to implement the InitializingBean interface, implement the afterPropertiesSet method, or specify the init-method in the configuration file.
2: The InitializingBean interface directly calls the afterPropertiesSet method, which is more efficient than calling the method specified by init-method through reflection. However, the init-method eliminates the dependency on spring.
3: if an error occurs when the afterPropertiesSet method is called, The method specified by init-method is not called.
4: TransactionTemplate implements the InitializingBean interface to determine whether transactionManager has been initialized. If not, an exception is thrown. The source code is as follows:
public void afterPropertiesSet() {
if (this.transactionManager == null) {
throw new IllegalArgumentException("Property 'transactionManager' is required");
}
}
ApplicationContextAware interface.
Here we need to understand that the single-Multi-instance mode is set in scope, otherwise the default is single-instance
This project is used because rest is built using Jersey and its controller does not need Spring management. Therefore, this interface is used to input beanName for loading during use.
public class BeanFactory implements ApplicationContextAware { public static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext context) throws BeansException { BeanFactory.context = context; } @SuppressWarnings("unchecked") public static
T getBean(String beanName) { return (T) context.getBean(beanName); }}
How to call multiple objects in a single example
Spring injection non-singleton bean and scope
When loading the Spring configuration file, if the Bean class defined in the Spring configuration file implements the ApplicationContextAware interface, the setApplicationContext method in the ApplicationContextAware interface is automatically called when loading the Spring configuration file, obtain the ApplicationContext object. Why?
In most cases, the beans in the container are of the singleton type. If a singleton bean references another singleton bean, or a non-singleton bean references another non-singleton bean, generally, you can define a bean as the property value of another bean. However, this will be problematic for beans with different lifecycles. For example, when calling A method of A singleton bean A, you need to reference another non-singleton (prototype) for bean A, the container is only created once, so that the container cannot provide A new bean B instance for bean A every time when necessary.
In this way, we can obtain the context of spring and further obtain the desired bean.
FactoryBean Interface
Reference
You can call the getBean method to generate a new object.
BeanFactory ends with a Factory. It is a Factory class used to manage beans.
FactoryBean: end with Bean, indicating that it is a Bean. Unlike normal beans, It is a Bean that implements the FactoryBean interface, the object returned by getObject () of FactoryBean instead of FactoryBean itself is actually obtained from BeanFactory Based on the Bean Id. If you want to obtain the FactoryBean object, you can add an & symbol before the id to obtain it. The proxy class is returned since then.
Since then, the manual interceptor is as follows:
1. Customize annotation @ Interceptor to write the Interceptor checks on the classes to be intercepted. The interceptor interface can include doBefore and doAfter. Parse the annotation to obtain the class to be used.
2. the proxy class inherits the ApplicationContextAware and FactoryBean interfaces. Process the annotation and generate the target object of the proxy object. Use a dynamic proxy package to set the proxy object to the actually used bean.
3. Use the dynamic proxy class.
BeanFactoryAware
Reference
Private BeanFactory beanFactory; public void setBeanFactory (BeanFactory beanFactory) throws BeansException {this. beanFactory = beanFactory;} // so that we can use something directly, such as Object object = beanFactory. getBean (beanName );
Since we can get the object we want through set, why do we need to use this beanFactory? The truth is very simple, because in some cases, we need to dynamically obtain the object, for example, I have 10 bank processing objects, all of which inherit my BankService objects, but which bank object is needed for specific processing? This depends on the user's choice. You can inject 10 BankService instances and then use if-else, but that is too cumbersome. Every time you add a bank, you need to change the code.