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

网友投稿 602 2023-01-11


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

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}

我们也可以将配置中的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://springframework.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

}

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

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

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

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.outofmemory

hellospring.properties.annotation

0.0.1-SNAPSHOT

jar

hellospring.properties.annotation

http://maven.apache.org

UTF-8

3.0.0.RC2

junit

junit

3.8.1

test

org.springframework

spring-context

${org.springframework-version}

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.outofmemory

hellospring.properties.annotation

0.0.1-SNAPSHOT

jar

hellospring.properties.annotation

http://maven.apache.org

UTF-8

3.0.0.RC2

junit

junit

3.8.1

test

org.springframework

spring-context

${org.springframework-version}

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

如下spring配置文件appContext.xml

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

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

http://springframework.org/schema/context

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

classpath*:application.properties

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

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

http://springframework.org/schema/context

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

classpath*:application.properties

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

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文件的内容如下:

mysql.url=mysql's url

mysql.userName=mysqlUser

mysql.password=mysqlPassword

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

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的实例,然后打印出来,运行程序会输出配置文件中配置的值

总结

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


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

上一篇:接口测试用例怎样设计(接口测试如何设计测试用例)
下一篇:Java实现的矩阵乘法示例
相关文章

 发表评论

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