spring实例:在xml中引入配置文件

Source

step.1

我们将模拟连接数据库的过程,在resources下新建一个我们要引入的配置文件database.propertiser如下:

driver =com.mysql.cj.jdbc.Driver
url = mysql:jdbc://localhost:3306//test?userUnicode=true&characterEncoding=utf-8&serverTimezone=UFC;
user =root
password = root 

step.2

新建ProviderDao类

package com.zt.dao;

public class ProviderDao {
    private String url ;
    private String driver;
    private String user;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "ProviderDao{" +
                "url='" + url + '\'' +
                ", driver='" + driver + '\'' +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

}

step.3

新建resources.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:aop="http://www.springframework.org/schema/aop"
               xmlns:c="http://www.springframework.org/schema/c"
               xmlns:cache="http://www.springframework.org/schema/cache"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:jdbc="http://www.springframework.org/schema/jdbc"
               xmlns:jee="http://www.springframework.org/schema/jee"
               xmlns:lang="http://www.springframework.org/schema/lang"
               xmlns:mvc="http://www.springframework.org/schema/mvc"
               xmlns:p="http://www.springframework.org/schema/p"
               xmlns:task="http://www.springframework.org/schema/task"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:util="http://www.springframework.org/schema/util"
               xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc ">


<!--通过该方式获取路径下的文件-->
<context:property-placeholder location="classpath:database.properties"/>

<bean class="com.zt.dao.ProviderDao" id="providerDao2">
    <!--${}表达式可以去应用我们去应用我们引用的这些properties(通过context:引进的)
    里面的属性的值通过它的键名来得到值-->
    <property name="driver" value="${driver}"/>
    <property name="user" value="${user}"/>
    <property name="url" value="${url}"/>
    <property name="password" value="${password}"/>
</bean>
</beans>

step4

测试:

@Test
    public void m1(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("resources.xml");
        ProviderDao providerDao = ctx.getBean("providerDao2", ProviderDao.class);
        System.out.println(providerDao);
    }

输出结果:ProviderDao{url='mysql:jdbc://localhost:3306//test?userUnicode=true&characterEncoding=utf-8&serverTimezone=UFC;', driver='com.mysql.cj.jdbc.Driver', user='root', password='root '}

引入配置文件成功,测试完毕