Spring---Bean uses external properties file

Source: Internet
Author: User

When configuring beans in a configuration file, it is sometimes necessary to mix the details of the system deployment in the bean's configuration (for example: file path, data source configuration information, etc.). And these deployment details actually need to be separated from the Bean configuration
Spring provides a propertyplaceholderconfigurer Beanfactory post processor that allows the user to move part of the Bean configuration out of the properties file. You can use the form ${var} in the Bean configuration file to assign a value to a variable, propertyplaceholderconfigurer a dependency file, and use these properties to replace the variable.
Spring also allows the use of ${propname} in property files to implement mutual references between attributes.

The bean uses the external properties file implementation steps (to configure the C3P0 data source as an example)

1 Create c3p0 External properties file Db.properties (using key-value pairs)


After 2spring4.0 is configured using the context namespace, you need to import the context namespace:


3 Configure the external file in the configuration file, and the bean instance of C3P0, with the following code:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">    <Context:property-placeholder Location= "Com/jeremy/spring/properties/c3po.properties"/>    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">        < Propertyname= "User"value= "${user}"></ Property>        < Propertyname= "Password"value= "${password}"></ Property>        < Propertyname= "Driverclass"value= "${driverclass}"></ Property>        < Propertyname= "Jdbcurl"value= "${jdbcurl}"></ Property>    </Bean></Beans>

Spring---Bean uses external properties file

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.