Spring Note--10. Two types of post processors

Source: Internet
Author: User

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:

    1. Constructor Build Instance

    2. Set method Setting value

    3. Postprocessbeforeinitialization method

    4. Afterpropertiesset interface method

    5. Init-method method

    6. 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:

    1. Propertyplaceholderconfigurer: Property Placeholder Configurator

    2. Propertyoverrideconfigurer: Overriding Placeholder Configurator

    3. Customautowireconfigurer: Custom Auto-Assembled Configurator

    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.