Spring依赖注入:在spring配置文件中使用properties配置文件,springproperties,spring的配置文件相
Spring依赖注入:在spring配置文件中使用properties配置文件,springproperties,spring的配置文件相
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); }}
运行程序,即可看到输出。
你可以下载demo项目的源码。
用户点评