Spring uses external properties file configuration

Source: Internet
Author: User

The 1.Spring provides a propertyplaceholderconfigurer Beanfactory post processor that allows the user to put part of the bean's configuration

Move to the properties file. You can load a property in a propertyplaceholderconfigurer dependency file by using a variable of ${var} in the bean configuration file.

General properties Files dot after property

Case: Configuration of the data source (configure information for the linked database)

In addition to the required packages for spring, we also import two rack packages

1. The driver package for the database

2.C3P0 's Rack Bag

Db-properties.xml

<?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.0.xsd
   http://www.springframework.org/schema/context
  /http Www.springframework.org/schema/context/spring-context-3.0.xsd ">  
 
 
       <!--Import Properties File   - 
    <context:property-placeholder location= "Classpath:db.properties"/>

<!--database link pool configuration--
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "user" value= "${user}" ></property>
<property name= "Password" value= "${password}" ></property>
<property name= "Driverclass" value= "${driverclass}" ></property>
<property name= "Jdbcurl" value= "${jdbcurl}" ></property>
</bean>
</beans>

<!--external properties File--->

Db. Properties
User=root
password=sys123456
Driverclass=com.mysql.jdbc.driver
Jdbcurl=jdbc:mysql:///xbc

Write a test method main, test whether the database is successfully linked

Package com.spring.db;

Import java.sql.SQLException;

Import Javax.sql.DataSource;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Dbtest {

public static void Main (string[] args) throws SQLException {
ApplicationContext ctx=new classpathxmlapplicationcontext ("Db-properties.xml");
DataSource DataSource = (DataSource) ctx.getbean ("DataSource");
System.out.println ("in Execution");
System.out.println (Datasource.getconnection ());
System.out.println ("End");
}



Spring uses external properties file configuration

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.