How to intervene in the spring startup process

Source: Internet
Author: User

Spring provides a container extension mechanism called Beanfactorypostprocessor. This mechanism allows us to modify the information saved by the Beandefinition registered to the container before the container instantiates the corresponding object. This is equivalent to adding a process at the end of the first phase of the container implementation, allowing us to do some extra work on the final beandefinition, such as modifying certain attributes of the bean definition, adding additional information to the bean definition, and so on. If you want to customize implementation beanfactorypostprocessor, we usually need to implement the Org.springframework.beans.factory.config.BeanFactoryPostProcessor interface.

The implementation class of common Beanfactorypostprocessor

1.PropertyPlaceholderConfigurer

Propertyplaceholderconfigurer allows us to use placeholders (PlaceHolder) in the XML configuration file, and the resources represented by these placeholders are individually configured into a simple properties file to load. With the data source configuration as an example, after using Propertyplaceholderconfigurer (which follows the configuration of listing 4-42), you can configure the data source in the XML configuration file as shown in Listing 4-43 instead of the connection address, The user name, password, and so on are all configured in XML.

Instance:

 <bean id= "DataSource"  class= "Org.apache.commons.dbcp.BasicDataSource"  destroy-method= " Close "> <property name=" url "> <value>${jdbc.url}</value> </property> <property& Nbsp;name= "Driverclassname" > <value>${jdbc.driver}</value> </property> <property  Name= "username" > <value>${jdbc.username}</value> </property> <property name= " Password "> <value>${jdbc.password}</value> </property> <property name=" Testonborrow " > <value>true</value> </property> <property name= "Testonreturn" > <value>true </value> </property> <property name= "Testwhileidle" > <value>true</value> </ Property></bean> 

2.PropertyOverrideConfigurer

Overwrite overrides can be overridden by Propertyoverrideconfigurer for any property information that is configured in the container that you want to work with the bean definition. This sounds more abstract, let's give an example! For example, in the previous DataSource definition, the value of maxactive is 100, if we think that 100 is not appropriate, then you can use the Propertyoverrideconfigurer in its corresponding properties file as shown in the following configuration, Overwrite the value of 100, as configured to 200:

Spring configuration file

<bean class= "Org.springframework.beans.factory.config.PropertyOverrideConfigurer" > <property name= " Location "value=" Pool-adjustment.properties "/></bean>
Pool-adjustment.properties File Information
# pool-adjustment.properties 11datasource.minevictableidletimemillis=1000datasource.maxactive=50

(Note:the properties file content format must be beanname.propertyname=value )

3, Customeditorconfigurer

XML is all about string types, that is, a container reads a string form from an XML-formatted file, and the final application is made up of various types of objects. To accomplish this kind of conversion from string to object (regardless of who ultimately does the conversion work), you need the information about the conversion rules, and customeditorconfigurer is helping us to convey similar information.

3.1 is to give a org.springframe.PropertyEditorRegistrar implementation.

  

Datepropertyeditorregistrar

Spring Configuration

<bean class= "Org.springframework.beans.factory.config.CustomEditorConfigurer" >         <property name= "Propertyeditorregistrars" >             <list>                 <ref bean= "Datepropertyeditorregistrar"/>             </list>         </property>    </bean>         <bean id= "Datepropertyeditorregistrar"  class= ".... Datepropertyeditorregistrar ">        <property name=" PropertyEditor ">            <ref bean=" Datepropertyeditor " />        </property>    </bean>         <bean id= "Datepropertyeditor"  class= ".... Datepropertyeditor ">        <property name=" DatePattern " >            <value>yyyy/mm/dd</value >        </property>    </bean>

How to intervene in the spring startup process

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.