Postprocessor: Post processor;
Beanpostprocessor:bean's post processor (the processing object is the bean);
Beanfactorypostprocessor:beanfactory Post Processor (processing object is beanfactory)
(1) Look at the source code of the Beanfactorypostprocessor interface:
@FunctionalInterfacepublic interface BeanFactoryPostProcessor { /** * Modify the application context‘s internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @param beanFactory the bean factory used by the application context * @throws org.springframework.beans.BeansException in case of errors */ void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;}
What is the role of the ① method Postprocessbeanfactory (configurablelistablebeanfactory beanfactory)?
The source code is as follows:
context‘s internal bean factory after its standard initialization.
Bean Factory has gone through the standard initialization;
The Configurablelistablebeanfactory Beanfactory of the method is the Bean factory after the standard initialization;
We can make changes to the Bean factory after the standard initialization is given to us for processing.
When did ② call Postprocessbeanfactory (Configurablelistablebeanfactory beanfactory)?
The source code is as follows:
// All bean definitions will have been loaded, but no beans will have been instantiated yet.
Bean Factory has gone through standard initialization, and all bean definitions have been loaded into the Bean factory, but no beans have been created (instantiated).
The beanfactory of the ③ method
The Configurablelistablebeanfactory Beanfactory of the method is the object we need to deal with; Beanfactory has gone through the standard initialization, all the beans Definitions has also been loaded into the beanfactory.
How does ④ operate (process) Beanfactory?
Common ways to manipulate beanfactory:
Modify the bean definitions that is loaded into beanfactory.
With parameter beanfactory, you can get the bean definition of the relevant bean and modify the bean definition.
(2) Code example:
User class:
PublicClass User {Private String username;Privateint age;PublicvoidSetusername (String username) {System.out.println ( "Setusername ()"); this.username = Username;} public void setAge (int age" {System. "Setage ()"); this.age = Age;} public user () {system. Out.println ( "Constructor ()"),} @Override public string tostring () {return "User [username=" + Username + "]";}
Beanfactorypostprocessor Processor:
PublicClassMybeanfactorypostprocessorImplementsBeanfactorypostprocessor {PublicMybeanfactorypostprocessor () {System.out.println ("Constructor of the Beanfactorypostprocessor implementation Class"); }@Override public void postprocessbeanfactory (configurablelistablebeanfactory beanfactory) Throws Beansexception {System.out.println ("beanfactorypostprocessor.postprocessbeanfactory ()"); //Get the specified beandefinition beandefinition bd = beanfactory.getbeandefinition ("user1"); //Get mutablepropertyvalues mutablepropertyvalues PVs = bd.getpropertyvalues () according to Beandefinition; //Increase Pvs.add ("age", 20); //Overwrite Pvs.add ("username", "Richard");}}
Bean configuration file:
<Bean id= "user1" class= "com. User > <!--This property username is assigned only, attribute age is not assigned-- < property name="username" value= "Jack" ></property> </bean> <bean class="com. Mybeanfactorypostprocessor "></bean>
Test class:
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user1=(User)context.getBean("user1"); System.out.println(user1); }
Operation Result:
Beanfactorypostprocessor Implementing a class constructor
Beanfactorypostprocessor.postprocessbeanfactory ()
Constructor ()
Setusername ()
Setage ()
User [Username=richard, age=20]
(3) Attention points:
The Beanfactorypostprocessor interface is very similar to the Beanpostprocessor interface, both of which are externally exposed when spring initializes the bean.
①beanfactory Post Processor: A special bean provided by spring
②beanfactory of the rear processor, need to implement the Beanfactorypostprocessor interface
③ needs to register the Beanfactory processor in the bean's configuration file, but does not need to set the id attribute. The IOC container automatically recognizes that this is a beanfactory post-processor that automatically uses it.
<bean class="com.MyBeanFactoryPostProcessor"></bean>
④ instantiates the Beanfactorypostprocessor implementation class and calls the Beanfactorypostprocessor postprocessbeanfactory () method;
These 2 steps will be done very early, before the bean instance is created.
⑤bean's post processor, which operates on beans, processes all the beans in the container, and each bean undergoes 2 processing methods postprocessbeforeinitialization () and Postprocessafterinitialization ();
Beanfactory's post processor, the object is beanfactory, so there will only be one processing (very early, before the bean instance is created), that is, only call the processing method Postprocessbeanfactory () once.
Spring's beanfactorypostprocessor and beanpostprocessor