Beanpostprocessor (Bean post processor) is commonly used to modify the value inside the bean, implement the bean dynamic proxy, and so on.
Both Beanfactorypostprocessor and Beanpostprocessor are the extension points exposed when spring initializes the bean. But what difference do they have?
The graph of the Understanding bean life cycle shows that Beanfactorypostprocessor is the earliest called in the life cycle, much earlier than Beanpostprocessor. It executes before the bean is instantiated, after the spring container loads the bean's definition file. That is, Spring allows beanfactorypostprocessor to read bean configuration metadata and modify it before the container creates a bean. For example, to increase the bean's properties and values, to reset the bean as a candidate for automatic assembly, resetting the bean's dependencies, and so on.
You can configure multiple beanfactorypostprocessor at the same time in the Srping configuration file and control the order of execution for each beanfactorypostprocessor by setting the ' order ' property when registering in XML.
The Beanfactorypostprocessor interface is defined as follows:
Public interface Beanfactorypostprocessor { void Postprocessbeanfactory (configurablelistablebeanfactory Beanfactory) throws beansexception;}
Interface has only one method postprocessbeanfactory. The parameter of this method is configurablelistablebeanfactory type, in actual development, we often use its getbeandefinition () method to get the metadata definition of a bean: beandefinition. It has these methods:
Look at an example:
A bean is defined in the configuration file:
<bean id= "Messi" class= "Twm.spring.LifecycleTest.footballPlayer" > <property name= "name" value= "Messi" ></property> <property name= "Team" value= "Barcelona" ></property></bean>
Create class Beanfactorypostprocessorimpl to implement Interface Beanfactorypostprocessor:
public class Beanfactorypostprocessorimpl implements beanfactorypostprocessor{public void Postprocessbeanfactory (Configurablelistablebeanfactory beanfactory) throws beansexception { System.out.println ("Beanfactorypostprocessorimpl"); Beandefinition bdefine=beanfactory.getbeandefinition ("Messi"); System.out.println (Bdefine.getpropertyvalues (). toString ()); Mutablepropertyvalues PV = bdefine.getpropertyvalues (); if (Pv.contains ("team")) { propertyvalue ppv= pv.getpropertyvalue ("name"); Typedstringvalue obj= (Typedstringvalue) ppv.getvalue (); if (Obj.getvalue (). Equals ("Messi")) { pv.addpropertyvalue ("team", "Argentina"); } } Bdefine.setscope (Beandefinition.scope_prototype); }}
Call class:
public static void Main (string[] args) throws Exception { ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Footballplayer obj = Ctx.getbean ("Messi", footballplayer.class); System.out.println (Obj.getteam ());}
Output:
propertyvalues:length=2; Bean property ' name '; Bean Property ' Team '
Argentina
The Propertyplaceholderconfigurer class referred to in the Propertyplaceholderconfigurer application is an implementation of the Beanfactorypostprocessor interface. It replaces the placeholder (such as ${jdbc.url}) in the class definition with the contents of the properties file before the container creates the bean.