5.spring框架-----通过xml加载properties文件

一、加载properties文件

新建一个properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bookshop
jdbc.username=root
jdbc.password=12345

在applicationContext.xml中开context命名空间

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

</beans>

加载properties配置文件

    <context:property-placeholder location="jdbc.properties"/>

使用${key}来读取properties配置文件中的内容并完成属性注入

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

添加对应的类 BookDao和BookDaoImpl类,并在BookDaoImpl类中添加name属性与setter方法

public interface BookDao {
    public void save();
}

public class BookDaoImpl implements BookDao {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void save() {
        System.out.println("book dao save ..." + name);
    }
}

配置文件 

    <context:property-placeholder location="jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
        <property name="name" value="${jdbc.driver}"/>
    </bean>

运行文件

public class AppForLifeCycle {
    public static void main( String[] args ) {
        ApplicationContext ctx = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);

        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }
}

标签会加载系统的环境变量,而且环境变量的值会被优先加载

配置文件

<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
        <property name="name" value="${username}"/>
    </bean>

properties文件新加一行配置

username=9999

添加对应的类 BookDao和BookDaoImpl类,并在BookDaoImpl类中添加name属性与setter方法

public interface BookDao {
    public void save();
}

public class BookDaoImpl implements BookDao {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void save() {
        System.out.println("book dao save ..." + name);
    }
}

运行文件

public class AppForLifeCycle {
    public static void main( String[] args ) {
        ApplicationContext ctx = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }
}

重新运行

 

这时看到输出的名称不是设置的9999

就是因为标签会加载系统的环境变量,而且环境 变量的值会被优先加载

system-properties-mode:设置为NEVER,表示不加载系统属性,就可以解决上述问题。

    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

 

当有多个properties配置文件需要被加载

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bookshop
jdbc.username=root
jdbc.password=12345

 jdbc1.properties

username=9999
        <!--方式一 -->
<context:property-placeholder
location="jdbc.properties,jdbc2.properties" system-propertiesmode="NEVER"/>
        <!--方式二-->
<context:property-placeholder location="*.properties" systemproperties-mode="NEVER"/>
        <!--方式三 -->
<context:property-placeholder location="classpath:*.properties"
                              system-properties-mode="NEVER"/>
        <!--方式四-->
<context:property-placeholder location="classpath*:*.properties"
                              system-properties-mode="NEVER"/>
        </beans>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习java的张三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值