Spring4---使用外部属性文件

观察问题

  • 在项目之中往往需要进行一些基本的配置信息部署,例如:数据库的连接,文件路径等等.这些都属于西东细节信息,应该与Bean文件分离,如果将数据库连接信息写在Bean文件之中,则维护起来相对麻烦

  • 示例:使用Bean文件配置数据库链接

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--使用c3op 连接数据库-->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接用户名-->
        <property name="user" value="root"/>
        <!--连接密码-->
        <property name="password" value="mysqladmin"/>
        <!--加载驱动程序类-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <!--连接路径-->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maoshu"/>

    </bean>
</beans>
  • 获取连接对象
package mao.shu.spring.database;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class DatabaseTest {
    private ApplicationContext app;

    @Before
    public void before(){
        this.app = new ClassPathXmlApplicationContext("database.xml");

    }
    @Test
    public void tesetGet() throws SQLException {
        ComboPooledDataSource com = (ComboPooledDataSource) this.app.getBean("comboPooledDataSource");
        System.out.println(com.getConnection());
    }
}

  • 虽然以上的方式可以获取到数据库的链接,但是直接将数据库链接信息写在Bean的配置文件中不是很好,因此可以利用*.property属性文件配置数据库连接信息

使用外部属性文件

在这里插入图片描述

注册 PropertyPlaceholderConfigurer

在这里插入图片描述

  • 示例:建立jdbc.properties文件
user=root
password=mysqladmin
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/maoshu
  • 修改database.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--读取的资源文件信息-->
    <context:property-placeholder location="jdbc.properties"/>

    <!--使用c3op 连接数据库-->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接用户名-->
        <property name="user" value="${user}"/>
        <!--连接密码-->
        <property name="password" value="${password}"/>
        <!--加载驱动程序类-->
        <property name="driverClass" value="${driver}"/>
        <!--连接路径-->
        <property name="jdbcUrl" value="${url}"/>

    </bean>
</beans>
  • 测试
package mao.shu.spring.database;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class DatabaseTest {
    private ApplicationContext app;

    @Before
    public void before(){
        this.app = new ClassPathXmlApplicationContext("database.xml");
    }



     @Test
    public void tesetGet() throws SQLException {
        ComboPooledDataSource com = (ComboPooledDataSource) this.app.getBean("comboPooledDataSource");
        System.out.println(com.getConnection());
        System.out.println(com.getConnection().getMetaData().getUserName());
        System.out.println(com.getConnection().getMetaData().getURL());
        System.out.println(com.getConnection().getMetaData().getDriverName());
    }
}

  • 输出结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43386754/article/details/88074382