一個系統(tǒng)中通常會存在如下一些以properties形式存在的配置文件
1.數(shù)據(jù)庫配置文件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.消息服務(wù)配置文件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.遠(yuǎn)程調(diào)用的配置文件demo-remote.properties:
1
2
3
|
remote.ip=localhost remote.port= 16800 remote.servicename=test |
一、系統(tǒng)中需要加載多個properties配置文件
應(yīng)用場景:properties配置文件不止一個,需要在系統(tǒng)啟動時同時加載多個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://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.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://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.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
應(yīng)用場景:工程組中有多個配置文件,但是這些配置文件在多個地方使用,所以需要分別加載。
配置如下:
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://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.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.commons.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,則需設(shè)置為true。這里一定需要按照這種方式設(shè)置這兩個參數(shù)。
三、bean中直接注入properties配置文件中的值
應(yīng)用場景: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: //www.springframework.org/schema/beans" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a>" xmlns:xsi= "<a href=" http: //www.w3.org/2001/xmlschema-instance" rel="external nofollow" >http://www.w3.org/2001/xmlschema-instance</a>" xmlns:util= "<a href=" http: //www.springframework.org/schema/util" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a>" xsi:schemalocation=" <a href= "http://www.springframework.org/schema/beans" rel= "external nofollow" rel= "external nofollow" >http://www.springframework.org/schema/beans</a> <a href= "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" rel= "external nofollow" >http://www.springframework.org/schema/beans/spring-beans- 3.0 .xsd</a> <a href= "http://www.springframework.org/schema/util" rel= "external nofollow" rel= "external nofollow" >http://www.springframework.org/schema/util</a> <a href= "http://www.springframework.org/schema/util/spring-util-3.0.xsd" rel= "external nofollow" >http://www.springframework.org/schema/util/spring-util- 3.0 .xsd</a>"> <!-- 這種加載方式可以在代碼中通過 @value 注解進行注入, 可以將配置整體賦給properties類型的類變量,也可以取出其中的一項賦值給string類型的類變量 --> <!-- <util:properties/> 標(biāo)簽只能加載一個文件,當(dāng)多個屬性文件需要被加載的時候,可以使用多個該標(biāo)簽 --> <util:properties id= "remotesettings" location= "file:/opt/demo/config/demo-remote.properties" /> <!-- <util:properties/> 標(biāo)簽的實現(xiàn)類是propertiesfactorybean, 直接使用該類的bean配置,設(shè)置其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類型的類變量
應(yīng)用場景:當(dāng)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://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 可以使用如下的方式聲明properties類型的factorybean來加載配置文件,這種方式就只能當(dāng)做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> <!-- 遠(yuǎn)端調(diào)用客戶端類 --> <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://www.w3.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://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 "> <!-- 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.component; @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文件的內(nèi)容如下:
1
2
3
|
mysql.url=mysql's url mysql.username=mysqluser mysql.password=mysqlpassword |
最后我們需要測試一下以上寫法是否有問題,如下app.java文件內(nèi)容:
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的實例,然后打印出來,運行程序會輸出配置文件中配置的值
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/qq_26562641/article/details/54601085