Spring学习笔记六--外部配置文件

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
     </bean>

    <!--
    导入配置文件src/datasource.properties
    -->
    <context:property-placeholder location="classpath:datasource.properties"/>
    <bean id="datasource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverclas}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
    </bean>

</beans>
package properties;

import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.SQLException;


public class Main {
    public static void main(String[] args) throws SQLException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("properties//property.xml");
        //DataSource dataSource = (DataSource) ctx.getBean("datasource");
        //System.out.println(dataSource.getConnection());

        DataSource dataSource1 = (DataSource) ctx.getBean("datasource1");
        System.out.println(dataSource1.getConnection());
    }
}
//datasource.properties
user=root
password=123456
driverclas=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///test


你可能感兴趣的:(Spring学习笔记六--外部配置文件)