Spring依赖注入:在spring配置文件中使用properties配置文件

spring的配置文件相对是固定的,通常作为资源文件存在,而程序中经常会有一些需要配置的变量,比如数据库的连接信息,memcached的地址信息等等,这些信息最好单独放在java的配置文件properties文件中。

下面我们通过一个例子演示如何在spring中使用properties文件中的配置。

首先新建一个maven项目,并在依赖中添加spring相关的项,如果不知道添加那些依赖,请参照上一篇文章。

建好项目之后,我们新建一个java的接口IDog,和这个接口的两个实现类CommonDog和BeautifulDog。IDog有两个方法getWeight()和run()方法:

package cn.outofmemory.hellospring.properties;public interface IDog {
    void run();
    int getWeight();}

CommonDog和BeatifulDog的实现如下:

package cn.outofmemory.hellospring.properties;public class CommonDog implements IDog {

    private int weight;

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getWeight() {
        return this.weight;
    }

    @Override
    public void run() {
        System.out.println("common dog running...");
    }}
package cn.outofmemory.hellospring.properties;public class BeautifulDog extends CommonDog implements IDog {

    @Override
    public void run() {
        System.out.println("running beautifully");
    }}

我们需要在spring.xml中配置App.java中要使用的IDog的实现类,和这个实现类的实例的weight值,我们先看下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-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
    <!-- define envirement varibles -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="pphc">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <!-- standard config -->
                <value>classpath*:application.properties</value>
            </list>
        </property>
    </bean>

    <bean id="appDog" class="${dog.type}">
        <property name="weight" value="${dog.weight}"/>
    </bean>
 </beans>

在这个配置文件中,我们首先定义了类型为org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的bean,这个bean用来将解析properties文件,spring可以使用此类,来将其他bean中类似EL表达式${abc}的值替换为配置文件的值。PropertyPlaceholderConfigurer类的locations属性是一个数组,也就是说可以指定多个配置文件。

spring.xml中的第二个bean,其id为appDog,而其具体的类型是${dog.type}是一个配置文件的项,他的weight属性的值是${dog.weight}也是一个配置项。

我们再看下配置文件application.properties的内容:

dog.weight=10dog.type=cn.outofmemory.hellospring.properties.BeautifulDog

最后我们看下以上配置是否可以正常的工作,在App.java类中添加如下代码:

package cn.outofmemory.hellospring.properties;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/**
 * Hello world!
 *
 */public class App {
    public static void main( String[] args )
    {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
        IDog appDog = (IDog)appContext.getBean("appDog");
        appDog.run();
        String weightMsg = String.format("appDog's weight is %d", appDog.getWeight());
        System.out.println(weightMsg);
    }}

运行程序,即可看到输出。


转载于:https://my.oschina.net/u/1859350/blog/530034

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值