Spring-bean configuration-Use external properties file

Source: Internet
Author: User

Spring-bean configuration-Use external properties file

Therefore, you can get the key-value of the configuration file by @value annotations and generate a profile bean. The way to use beans directly in your code.

• 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.). 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 variables of the form ${var} in the bean configuration file. Propertyplaceholderconfigurer a dependency file, and use these properties to replace the variable. spring also allows you to use ${propname} in a property file to enable mutual references between properties. Case: Use Db.properties to configure the connection database information, obtain the information through the bean configuration file, and then establish the data source; The Db.properties configuration information is as follows: [Plain]View PlainCopy
    1. User=scott
    2. Password=tiger
    3. Dirverclass=oracle.jdbc.driver.oracledriver
    4. Jdbcurl=jdbc\:oracle\:thin\: @localhost \:1521\:oracl
Beans configuration file: Applicationcontext_properties.xml [HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans.xsd
  6. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd ">
  7. <!--Import Properties file --
  8. <context:property-placeholder location="classpath:db.properties"/>
  9. <Bean id= "dataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource">
  10. <!--using properties of external properties files--
  11. < name="user" value="${user}"> </Property>
  12. < name= "password" value="${password}"> </Property>
  13. <property name= "driverclass" value="${dirverclass}"></ Property>
  14. < name= "jdbcurl" value="${jdbcurl}"> </Property>
  15. </Bean>
  16. </Beans>
Create the data source code as follows: [Java]View PlainCopy
    1. ApplicationContext axt = new Classpathxmlapplicationcontext ("Applicationcontext_properties.xml");
    2. DataSource DataSource = (DataSource) axt.getbean ("DataSource");
    3. System.out.println (Datasource.getconnection ());

Spring-bean configuration-Use external properties file (GO)

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.