java加载多个properties文件_Spring加载配置和读取多个Properties文件的讲解

本文详细介绍了如何在Spring中加载并读取多个Properties文件,包括数据库、消息服务和远程调用的配置。通过XML配置、Bean注入和注解方式,展示了在不同场景下加载和使用配置文件的方法,提供了实际的代码示例。
摘要由CSDN通过智能技术生成

一个系统中通常会存在如下一些以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://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">

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

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

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}

注意:其中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://www.springframework.org/schema/beans"

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

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

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}

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

如下spring配置文件appContext.xml

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

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

总结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值