Spring strategy learning notes (2.12) -- External bean Configuration

Source: Internet
Author: User

I. knowledge points

In spring, propertyplaceholderconfigurer is the Bean Factory post-processor used to separately put some bean configurations into an attribute file. Variables in the form of $ {var} can be used in the bean configuration file. propertyplaceholderconfigurer loads attributes from the attribute file and replaces the variables with them.

The difference between the Bean Factory post-processor and the bean post-processor is that its objective is the IOC container --------- Bean factory or application context, rather than the bean instance. The Bean Factory post-processor takes effect after the bean configuration is loaded in the IOC container and before the bean instance is created. Its typical purpose is to modify the bean configuration before bean instantiation. Spring has multiple Bean Factory backprocessors.

Ii. Sample Code

Property file config. properties, placement path

cashier.path=c:/cashier

Bean Configuration

<! -- Register the Bean Factory post-processor propertyplaceholderconfigurer, configure an attribute file in the location attribute, and configure multiple attribute files for the locations attribute --> <Bean class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" location "> <value> config. properties </value> </property> </bean> <bean id = "cashier1" class = "com. codeproject. jackie. springrecipesnote. springadvancedioc. cashier "init-method =" openfile "Destroy-method =" closefile "> <property name =" path "value =" $ {cashier. path} "/> </bean>

In spring2.5 and later versions, you can use<Context: Property-placeholder> elementRegister propertyplaceholderpolicer.

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-co Ntext-3.2.xsd "> <! -- Register the bean post-Processor --> <Bean class = "com. codeproject. Jackie. springrecipesnote. springadvancedioc. pathcheckingbeanpostprocessor"/> ........... <Context: Property-placeholder location = "config. properties "/> <bean id =" cashier1 "class =" com. codeproject. jackie. springrecipesnote. springadvancedioc. cashier "init-method =" openfile "Destroy-method =" closefile "> <property name =" path "value =" $ {cashier. path} "/> </bean> </beans>

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.