Spring中配置和读取多个Properties文件
java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料
保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。
一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
Properties代码 ![]()
database.url=jdbc:mysql://localhost/smaple
database.driver=com.mysql.jdbc.Driver
database.user=root
database.password=123
2.消息服务配置文件demo-mq.properties:
Properties代码 ![]()
#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:
Properties代码 ![]()
remote.ip=localhost
remote.port=16800
remote.serviceName=test
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
Xml代码 ![]()
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
我们也可以将配置中的List抽取出来:
Xml代码 ![]()
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
二、整合多工程下的多个分散的Properties
应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。
配置如下:
Xml代码 ![]()
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">
p:password="${demo.db.password}" pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"
p:poolPreparedStatements="true" p:defaultAutoCommit="false">
注意:其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。这里一定需要按照这种方式设置这两个参数。
三、Bean中直接注入Properties配置文件中的值
应用场景:Bean中需要直接注入Properties配置文件中的值 。例如下面的代码中需要获取上述demo-remote.properties中的值:
Java代码 ![]()
public class Client() {
private String ip;
private String port;
private String service;
}
配置如下:
Xml代码 ![]()
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
Client类中使用Annotation如下:
Java代码 ![]()
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. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下
Java代码 ![]()
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并且注入
Xml代码 ![]()
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
代码如下:
Java代码 ![]()
import org.springframework.beans.factory.annotation.Autowired;
public class Client() {
//@Autowired也可以使用
private Properties remoteSettings;
//getter setter
}
上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。
propertiesspring
因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。
后续会有更多的精彩的内容分享给大家。
支付宝扫一扫
微信扫一扫
