好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring加载配置和读取多个Properties文件的讲解

一个系统中通常会存在如下一些以properties形式存在的配置文件

1.数据库配置文件demo-db.properties:

?

1

2

3

4

database.url=jdbc:mysql: //localhost/smaple

database.driver=com.mysql.jdbc.driver

database.user=root

database.password= 123

2.消息服务配置文件demo-mq.properties:

?

1

2

3

4

5

6

#congfig of activemq

mq.java.naming.factory.initial=org.apache.activemq.jndi.activemqinitialcontextfactory

mq.java.naming.provider.url=failover:(tcp: //localhost:61616?sotimeout=30000&connectiontimeout=30000)?jms.useasyncsend=true&timeout=30000

mq.java.naming.security.principal=

mq.java.naming.security.credentials=

jms.mailnotifyqueue.consumer= 5

3.远程调用的配置文件demo-remote.properties:

?

1

2

3

remote.ip=localhost

remote.port= 16800

remote.servicename=test

一、系统中需要加载多个properties配置文件

应用场景:properties配置文件不止一个,需要在系统启动时同时加载多个properties文件。

配置方式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

   xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

   xsi:schemalocation="

   http: //HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- 将多个配置文件读取到容器中,交给spring管理 -->

   <bean id= "propertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" >

     <property name= "locations" >

       <list>

        <!-- 这里支持多种寻址方式:classpath和file -->

        <value>classpath:/opt/demo/config/demo-db.properties</value>

        <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->

        <value>file:/opt/demo/config/demo-mq.properties</value>

        <value>file:/opt/demo/config/demo-remote.properties</value>

       </list>

     </property>

   </bean>

   <!-- 使用mq中的配置 -->

   <bean id= "mqjnditemplate" class = "org.springframework.jndi.jnditemplate" >

     <property name= "environment" >

       <props>

         <prop key= "java.naming.factory.initial" >${mq.java.naming.factory.initial}</prop>

         <prop key= "java.naming.provider.url" >${mq.java.naming.provider.url}</prop>

         <prop key= "java.naming.security.principal" >${mq.java.naming.security.principal}</prop>

         <prop key= "java.naming.security.credentials" >${mq.java.naming.security.credentials}</prop>

         <prop key= "username" >${mq.java.naming.security.principal}</prop>

         <prop key= "password" >${mq.java.naming.security.credentials}</prop>

       </props>

     </property>

   </bean>

</beans>

我们也可以将配置中的list抽取出来:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

   xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

   xsi:schemalocation="

   http: //HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- 将多个配置文件位置放到列表中 -->

   <bean id= "propertyresources" class = "java.util.arraylist" >

     <constructor-arg>

       <list>

        <!-- 这里支持多种寻址方式:classpath和file -->

        <value>classpath:/opt/demo/config/demo-db.properties</value>

        <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->

        <value>file:/opt/demo/config/demo-mq.properties</value>

        <value>file:/opt/demo/config/demo-remote.properties</value>

       </list>

     </constructor-arg>

   </bean>

   <!-- 将配置文件读取到容器中,交给spring管理 -->

   <bean id= "propertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" >

     <property name= "locations" ref= "propertyresources" />

   </bean>

   <!-- 使用mq中的配置 -->

   <bean id= "mqjnditemplate" class = "org.springframework.jndi.jnditemplate" >

     <property name= "environment" >

       <props>

         <prop key= "java.naming.factory.initial" >${mq.java.naming.factory.initial}</prop>

         <prop key= "java.naming.provider.url" >${mq.java.naming.provider.url}</prop>

         <prop key= "java.naming.security.principal" >${mq.java.naming.security.principal}</prop>

         <prop key= "java.naming.security.credentials" >${mq.java.naming.security.credentials}</prop>

         <prop key= "username" >${mq.java.naming.security.principal}</prop>

         <prop key= "password" >${mq.java.naming.security.credentials}</prop>

       </props>

     </property>

   </bean>

</beans>

二、整合多工程下的多个分散的properties

应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。

配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

   xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

   xmlns:p= "http://HdhCmsTestspringframework.org/schema/p"

   xsi:schemalocation="

   http: //HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- 将db属性配置文件位置放到列表中 -->

   <bean id= "dbresources" class = "java.util.arraylist" >

     <constructor-arg>

     <list>

       <value>file:/opt/demo/config/demo-db.properties</value>

     </list>

     </constructor-arg>

   </bean>

   <!-- 将mq属性配置文件位置放到列表中 -->

   <bean id= "mqresources" class = "java.util.arraylist" >

     <constructor-arg>

     <list>

       <value>file:/opt/demo/config/demo-mq.properties</value>

     </list>

     </constructor-arg>

   </bean>

   <!-- 用spring加载和管理db属性配置文件 -->

   <bean id= "dbpropertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" >

     <property name= "order" value= "1" />

     <property name= "ignoreunresolvableplaceholders" value= "true" /> 

     <property name= "locations" ref= "dbresources" />

   </bean>

   <!-- 用spring加载和管理mq属性配置文件 -->

   <bean id= "mqpropertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" >

     <property name= "order" value= "2" />

     <property name= "ignoreunresolvableplaceholders" value= "true" /> 

     <property name= "locations" ref= "mqresources" />

   </bean>

   <!-- 使用db中的配置属性 -->

   <bean id= "rmsdatasource" class = "org.apache测试数据mons.dbcp.basicdatasource" destroy-method= "close"

     p:driverclassname= "${demo.db.driver}" p:url= "${demo.db.url}" p:username= "${demo.db.username}"

     p:password= "${demo.db.password}" pp:maxactive= "${demo.db.maxactive}" p:maxwait= "${demo.db.maxwait}"

     p:poolpreparedstatements= "true" p:defaultautocommit= "false" >

   </bean>

   <!-- 使用mq中的配置 -->

   <bean id= "mqjnditemplate" class = "org.springframework.jndi.jnditemplate" >

     <property name= "environment" >

       <props>

         <prop key= "java.naming.factory.initial" >${mq.java.naming.factory.initial}</prop>

         <prop key= "java.naming.provider.url" >${mq.java.naming.provider.url}</prop>

         <prop key= "java.naming.security.principal" >${mq.java.naming.security.principal}</prop>

         <prop key= "java.naming.security.credentials" >${mq.java.naming.security.credentials}</prop>

         <prop key= "username" >${mq.java.naming.security.principal}</prop>

         <prop key= "password" >${mq.java.naming.security.credentials}</prop>

       </props>

     </property>

   </bean>

</beans>

注意:其中order属性代表其加载顺序,而ignoreunresolvableplaceholders为是否忽略不可解析的 placeholder,如配置了多个propertyplaceholderconfigurer,则需设置为true。这里一定需要按照这种方式设置这两个参数。

三、bean中直接注入properties配置文件中的值

应用场景:bean中需要直接注入properties配置文件中的值 。例如下面的代码中需要获取上述demo-remote.properties中的值:

?

1

2

3

4

5

public class client() {

   private string ip;

   private string port;

   private string service;

}

配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "<a href=" http: //HdhCmsTestspringframework.org/schema/beans" rel="external nofollow" rel="external nofollow" >http://HdhCmsTestspringframework.org/schema/beans</a>"

  xmlns:xsi= "<a href=" http: //HdhCmsTestw3.org/2001/xmlschema-instance" rel="external nofollow" >http://HdhCmsTestw3.org/2001/xmlschema-instance</a>"

  xmlns:util= "<a href=" http: //HdhCmsTestspringframework.org/schema/util" rel="external nofollow" rel="external nofollow" >http://HdhCmsTestspringframework.org/schema/util</a>"

  xsi:schemalocation="

  <a href= "http://HdhCmsTestspringframework.org/schema/beans" rel= "external nofollow" rel= "external nofollow" >http://HdhCmsTestspringframework.org/schema/beans</a> <a href= "http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd" rel= "external nofollow" >http://HdhCmsTestspringframework.org/schema/beans/spring-beans- 3.0 .xsd</a>

  <a href= "http://HdhCmsTestspringframework.org/schema/util" rel= "external nofollow" rel= "external nofollow" >http://HdhCmsTestspringframework.org/schema/util</a> <a href= "http://HdhCmsTestspringframework.org/schema/util/spring-util-3.0.xsd" rel= "external nofollow" >http://HdhCmsTestspringframework.org/schema/util/spring-util- 3.0 .xsd</a>">

  <!-- 这种加载方式可以在代码中通过 @value 注解进行注入, 

  可以将配置整体赋给properties类型的类变量,也可以取出其中的一项赋值给string类型的类变量 -->

  <!-- <util:properties/> 标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签 -->

  <util:properties id= "remotesettings" location= "file:/opt/demo/config/demo-remote.properties" /> 

  <!-- <util:properties/> 标签的实现类是propertiesfactorybean,

  直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的 -->

  <bean id= "settings"

class = "org.springframework.beans.factory.config.propertiesfactorybean" >

   <property name= "locations" >

  <list>

   <value>file:/opt/rms/config/rms-mq.properties</value>

   <value>file:/opt/rms/config/rms-env.properties</value>

  </list>

   </property>

  </bean>

</beans>

client类中使用annotation如下:

?

1

2

3

4

5

6

7

8

9

import org.springframework.beans.factory.annotation.value;

public class client() {

   @value ( "#{remotesettings['remote.ip']}" )

   private string ip;

   @value ( "#{remotesettings['remote.port']}" )

   private string port;

   @value ( "#{remotesettings['remote.servicename']}" )

   private string service;

}

四、bean中存在properties类型的类变量

应用场景:当bean中存在properties类型的类变量需要以注入的方式初始化

1. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下

?

1

2

3

4

5

6

import org.springframework.beans.factory.annotation.value;

import org.springframework.beans.factory.annotation.autowired;

public class client() {

   @value ( "#{remotesettings}" )

   private properties remotesettings;

}

2. 配置方式:也可以使用xml中声明bean并且注入

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

   xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

   xsi:schemalocation="

   http: //HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- 可以使用如下的方式声明properties类型的factorybean来加载配置文件,这种方式就只能当做properties属性注入,而不能获其中具体的值 -->

   <bean id= "remoteconfigs" class = "org.springframework.beans.factory.config.propertiesfactorybean" >

     <property name= "locations" >

       <list>

         <value>file:/opt/demo/config/demo-remote.properties</value>

       </list>

     </property>

   </bean>

   <!-- 远端调用客户端类 -->

   <bean id= "client" class = "com.demo.remote.client" >

     <property name= "properties" ref= "remoteconfigs" />

   </bean>

</beans>

代码如下:

?

1

2

3

4

5

6

import org.springframework.beans.factory.annotation.autowired;

public class client() {

   //@autowired也可以使用

   private properties remotesettings;

   //getter setter

}

上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。

在很多情况下我们需要在配置文件中配置一些属性,然后注入到bean中,spring提供了 org.springframework.beans.factory.config.preferencesplaceholderconfigurer 类,可以方便我们使用注解直接注入properties文件中的配置。

下面我们看下具体如何操作:

首先要新建maven项目,并在pom文件中添加spring依赖,如下pom.xml文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

  xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

  <modelversion> 4.0 . 0 </modelversion>

  <groupid>cn.outofmemory</groupid>

  <artifactid>hellospring.properties.annotation</artifactid>

  <version> 0.0 . 1 -snapshot</version>

  <packaging>jar</packaging>

  <name>hellospring.properties.annotation</name>

  <url>http: //maven.apache.org</url>

  <properties>

   <project.build.sourceencoding>utf- 8 </project.build.sourceencoding>

   <org.springframework-version> 3.0 . 0 .rc2</org.springframework-version>

  </properties>

  <dependencies>

   <dependency>

    <groupid>junit</groupid>

    <artifactid>junit</artifactid>

    <version> 3.8 . 1 </version>

    <scope>test</scope>

   </dependency>      

   <!-- spring -->

   <dependency>

     <groupid>org.springframework</groupid>

     <artifactid>spring-context</artifactid>

     <version>${org.springframework-version}</version>

   </dependency>

  </dependencies>

</project>

要自动注入properties文件中的配置,需要在spring配置文件中添加 org.springframework.beans.factory.config.propertiesfactorybean 和 org.springframework.beans.factory.config.preferencesplaceholderconfigurer 的实例配置:

如下spring配置文件appcontext.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

  xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemalocation="http: //HdhCmsTestspringframework.org/schema/beans

  http: //HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd

  http: //HdhCmsTestspringframework.org/schema/context

  http: //HdhCmsTestspringframework.org/schema/context/spring-context-3.0.xsd ">

   <!-- bean annotation driven -->

   <context:annotation-config />

   <context:component-scan base- package = "cn.outofmemory.hellospring.properties.annotation" >

   </context:component-scan>

   <bean id= "configproperties" class = "org.springframework.beans.factory.config.propertiesfactorybean" >

     <property name= "locations" >

       <list>

         <value>classpath*:application.properties</value>

       </list>

     </property>

   </bean>

   <bean id= "propertyconfigurer" class = "org.springframework.beans.factory.config.preferencesplaceholderconfigurer" >

     <property name= "properties" ref= "configproperties" />

   </bean> 

</beans>

在这个配置文件中我们配置了注解扫描,和 configproperties 实例和 propertyconfigurer 实例。这样我们就可以在java类中自动注入配置了,我们看下java类中如何做:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

package cn.outofmemory.hellospring.properties.annotation;

import org.springframework.beans.factory.annotation.value;

import org.springframework.stereotype测试数据ponent;

@component

public class mysqlconnectioninfo {

   @value ( "#{configproperties['mysql.url']}" )

   private string url;

   @value ( "#{configproperties['mysql.username']}" )

   private string username;

   @value ( "#{configproperties['mysql.password']}" )

   private string password;

   /**

    * @return the url

    */

   public string geturl() {

     return url;

   }

   /**

    * @return the username

    */

   public string getusername() {

     return username;

   }

   /**

    * @return the password

    */

   public string getpassword() {

     return password;

   }

}

自动注入需要使用@value注解,这个注解的格式 #{configproperties['mysql.url']} 其中configproperties是我们在appcontext.xml中配置的beanid,mysql.url是在properties文件中的配置项。

properties文件的内容如下:

?

1

2

3

mysql.url=mysql's url

mysql.username=mysqluser

mysql.password=mysqlpassword

最后我们需要测试一下以上写法是否有问题,如下app.java文件内容:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package cn.outofmemory.hellospring.properties.annotation;

import org.springframework.context.applicationcontext;

import org.springframework.context.annotation.annotationconfigapplicationcontext;

import org.springframework.context.support.classpathxmlapplicationcontext;

/**

  * hello world!

  *

  */

public class app

{

   public static void main( string[] args )

   {

     applicationcontext appcontext = new classpathxmlapplicationcontext( "appcontext.xml" );

     mysqlconnectioninfo conninfo = appcontext.getbean(mysqlconnectioninfo. class );

     system.out.println(conninfo.geturl());

     system.out.println(conninfo.getusername());

     system.out.println(conninfo.getpassword());

   }

}

在main方法中首先声明了appcontext,然后获得了自动注入的mysqlconnectioninfo的实例,然后打印出来,运行程序会输出配置文件中配置的值

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/qq_26562641/article/details/54601085

查看更多关于Spring加载配置和读取多个Properties文件的讲解的详细内容...

  阅读:13次