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