Spring中配置和读取多个Properties文件的方式方法

网友投稿 348 2023-05-24


Spring中配置和读取多个Properties文件的方式方法

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

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

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

database.driver=com.mysql.jdbc.Driver

database.user=root

database.password=123

2.消息服务配置文件demo-mq.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:

remote.ip=localhost

remote.port=16800

remote.serviceName=test

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

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

配置方式:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

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

classpzNvcmsath:/opt/demo/config/demo-db.properties

file:/opt/demo/config/demo-mq.properties

file:/opt/demo/config/demo-remote.properties

${mq.java.naming.factory.initial}

${mq.java.naming.provider.url}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

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

classpzNvcmsath:/opt/demo/config/demo-db.properties

file:/opt/demo/config/demo-mq.properties

file:/opt/demo/config/demo-remote.properties

${mq.java.naming.factory.initial}

${mq.java.naming.provider.url}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

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

classpath:/opt/demo/config/demo-db.properties

file:/opt/demo/config/demo-mq.properties

file:/opt/demo/config/demo-remote.properties

${mq.java.naming.factory.initial}

${mq.java.naming.provider.url}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

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

classpath:/opt/demo/config/demo-db.properties

file:/opt/demo/config/demo-mq.properties

file:/opt/demo/config/demo-remote.properties

${mq.java.naming.factory.initial}

${mq.java.naming.provider.url}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

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

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

配置如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

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

xsi:schemaLocation="

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

file:/opt/demo/config/demo-db.properties

file:/opt/demo/config/demo-mq.properties

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">

${mq.java.naming.factory.initial}

${mq.java.naming.provider.url}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

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

xsi:schemaLocation="

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

file:/opt/demo/config/demo-db.properties

file:/opt/demo/config/demo-mq.properties

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">

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">

${mq.java.naming.factory.initial}

${mq.java.naming.provider.url}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

${mq.java.naming.security.principal}

${mq.java.naming.security.credentials}

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

三、Bean中直接注入Properties配置文件中的值

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

public class Client() {

private String ip;

private String port;

private String service;

}

配置如下:

http://shttp://pringframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="

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

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.0.xsd">

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

file:/opt/rms/config/rms-mq.properties

file:/opt/rms/config/rms-env.properties

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

file:/opt/rms/config/rms-mq.properties

file:/opt/rms/config/rms-env.properties

Client类中使用Annotation如下:

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. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下

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并且注入

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

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

file:/opt/demo/config/demo-remote.properties

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

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

file:/opt/demo/config/demo-remote.properties

代码如下:

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

public class Client() {

//@Autowired也可以使用

private Properties remoteSettings;

//getter setter

}

五、使用通配符加载多个properties文件

或者


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于SpringBoot与Mybatis实现SpringMVC Web项目
下一篇:angular学习之ngRoute路由机制
相关文章

 发表评论

暂时没有评论,来抢沙发吧~