We can extend the IOC container through the Bean post processor and the container post processor. The former can enhance the function of the bean, the latter can enhance the function of the container.
Bean Post Processor
This is a special kind of bean, does not provide service, also does not need the id attribute, only is responsible for the other normal bean post processing, for example, the bean in the container generation agent and so on. It is created after the normal bean is created and must implement the Beanpostprocessor interface . The two methods inside are called after the target bean is initialized.
With ApplicationContext as a container, the schema automatically scans which is the Bean post processor. It is valid for each normal bean. Although it is called a post-processor, it is the time of the bean's creation before and after it is created. The order of execution is:
Constructor Build Instance
Set method Setting value
Postprocessbeforeinitialization method
Afterpropertiesset interface method
Init-method method
Postprocessafterinitialization method
If you use the Beanfactory container, you need to manually register the bean post processor. You need to now use the container to get the Bean post processor by ID and then add beanpostprocessor to Beanfactory.
The spring architecture also provides two commonly used post-processors. Beannameautoproxycreator can create a proxy for a bean instance based on the bean instance's Name property. Defaultadvisorautoproxycreator is able to create proxies for all bean instances in the container based on the advisor provided. The so-called Agent Bean is to enhance the target bean and modify its function to get the new bean.
Container Post processor
The Bean Post processor is responsible for processing all the beans in the container, and the container post processor is responsible for handling the container itself. The container post processor must implement the Postprocessbeanfactory method of the Beanfactorypostprocessor interface species , The method body is the processing of the container (ApplicationContext or Beanfactory), and the container can be expanded. ApplicationContext can also automatically detect containers after the processor disease automatic registration. Beanfactory is also required to register manually. Configure it in XML, just think of it as a normal bean. The spring architecture provides several commonly used container post processors:
Propertyplaceholderconfigurer: Property Placeholder Configurator
Propertyoverrideconfigurer: Overriding Placeholder Configurator
Customautowireconfigurer: Custom Auto-Assembled Configurator
Customscopeconfigurer: Customize the Configurator for use
Property Placeholder Configurator
You can read the order of the Properties property file and set these properties to the data of the spring configuration file.
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderCongifurer" > <property name= "Locations" > <list> <value>dbconn.properties</ value> <value>1.properties< /value> <value>2.properties </value> </list> </ property></bean><!--define the data source bean--><bean id= "DataSource" class= " Com.mchange.v2.c3p0.ComboPooledDataSource " destroy-method=" Close " p:driverclass=" $ {jdbc.driverclassname} " p:jdbcurl=" ${jdbc.url} " p:user=" ${ Jdbc.username} " p:password="${jdbc.password} "/>
${jdbc.url} This notation causes the schema to look for a property named Jdbc.url in the properties file read above, and then takes out the property value. This way we do not need to manipulate the XML file, just modify the properties file to control the global constants.
overriding Placeholder Configurator
Propertyoverrideconfigurer is another container that spring provides after the processor. The information specified by its properties file can directly overwrite the metadata in the XML. The key in its properties file should have the following format: Beanid.property. For example, the examples in the book are written like this:
Datasource.user=root
Datasource.password=123
If this is the case, the schema will automatically pay root and 123 to the user and password property of the bean with the ID named DataSource. This is more convenient than the post processor above, and the code in XML is more concise.
<bean class= "Org.springframework.beans.factory.config.PropertyOverrideCongifurer" > <property name= " Locations "> <list> <value>dbconn.properties</value> </list> </pr Operty></bean><bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close"/>
The properties file is as follows:
Datasource.user=rootdatasource.password=123
When multiple overrides exist at the same time, the last one is valid. The following schema configuration allows you to configure the override placeholder Configurator more succinctly.
<context:property-override location= "Classpath:db.properties"/>
This article from "Fingertip Light Fly" blog, declined reprint!
Spring note--10. Two kinds of post processors