- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring加载配置和读取多个Properties文件的讲解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一个系统中通常会存在如下一些以properties形式存在的配置文件 。
1.数据库配置文件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.消息服务配置文件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.远程调用的配置文件demo-remote.properties:
1
2
3
|
remote.ip=localhost
remote.port=
16800
remote.servicename=test
|
1、系统中需要加载多个properties配置文件 。
应用场景:properties配置文件不止一个,需要在系统启动时同时加载多个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>
|
2、整合多工程下的多个分散的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
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,则需设置为true。这里一定需要按照这种方式设置这两个参数.
3、bean中直接注入properties配置文件中的值 。
应用场景: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/> 标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签 -->
<util:properties id=
"remotesettings"
location=
"file:/opt/demo/config/demo-remote.properties"
/>
<!-- <util:properties/> 标签的实现类是propertiesfactorybean,
直接使用该类的bean配置,设置其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;
}
|
4、bean中存在properties类型的类变量 。
应用场景:当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来加载配置文件,这种方式就只能当做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>
<!-- 远端调用客户端类 -->
<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文件的内容如下:
1
2
3
|
mysql.url=mysql's url
mysql.username=mysqluser
mysql.password=mysqlpassword
|
最后我们需要测试一下以上写法是否有问题,如下app.java文件内容:
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的实例,然后打印出来,运行程序会输出配置文件中配置的值 。
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持。如果你想了解更多相关内容请查看下面相关链接 。
原文链接:https://blog.csdn.net/qq_26562641/article/details/54601085 。
最后此篇关于Spring加载配置和读取多个Properties文件的讲解的文章就讲到这里了,如果你想了解更多关于Spring加载配置和读取多个Properties文件的讲解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想要显示正在加载的 .gif,直到所有内容都已加载,包括嵌入的 iframe。但是,目前加载 gif 会在除 iframe 之外的所有内容都已加载后消失。我怎样才能让它等到 iframe 也加载完毕
首先,这是我第一次接触 Angular。 我想要实现的是,我有一个通知列表,我必须以某种方式限制 limitTo,因此元素被限制为三个,在我单击按钮后,其余的应该加载。 我不明白该怎么做: 设置“ V
我正在尝试在我的设备上运行这个非常简单的应用程序(使用 map API V2),并且出于某种原因尝试使用 MapView 时: 使用 java 文件: public class MainMap e
我正在使用 Python 2.6、Excel 2007 Professional 和最新版本的 PyXLL。在 PyXLL 中加载具有 import scipy 抛出异常,模块未加载。有没有人能够在
我想做这个: 创建并打包原始游戏。然后我想根据原始游戏中的蓝图创建具有新网格/声音/动画和蓝图的其他 PAK 文件。原始游戏不应该知道有关其他网格/动画/等的任何信息。因此,我需要在原始游戏中使用 A
**摘要:**在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法。 本文分享自华为云社区《【Java】读取/加载 properties配置文件的几种方法》,作者:Copy工程师。
在 Groovy 脚本中是否可以执行条件导入语句? if (test){ import this.package.class } else { import that.package.
我正在使用 NVidia 视觉分析器(来自 CUDA 5.0 beta 版本的基于 eclipse 的版本)和 Fermi 板,我不了解其中两个性能指标: 全局加载/存储效率表示实际内存事务数与请求事
有没有办法在通过 routeProvider 加载特定 View 时清除 Angular JS 存储的历史记录? ? 我正在使用 Angular 创建一个公共(public)安装,并且历史会积累很多,
使用 Xcode 4.2,在我的应用程序中, View 加载由 segue 事件触发。 在 View Controller 中首先调用什么方法? -(void) viewWillAppear:(BOO
我在某些Django模型中使用JSONField,并希望将此数据从Oracle迁移到Postgres。 到目前为止,当使用Django的dumpdata和loaddata命令时,我仍然没有运气来保持J
创建 Nib 时,我需要创建两种类型:WindowNib 或 ViewNib。我看到的区别是,窗口 Nib 有一个窗口和一个 View 。 如何将 View Nib 加载到另一个窗口中?我是否必须创建
我想将多个env.variables转换为静态结构。 我可以手动进行: Env { is_development: env::var("IS_DEVELOPMENT")
正如我从一个测试用例中看到的:https://godbolt.org/z/K477q1 生成的程序集加载/存储原子松弛与普通变量相同:ldr 和 str 那么,宽松的原子变量和普通变量之间有什么区别吗
我有一个重定向到外部网站的按钮/链接,但是外部网站需要一些时间来加载。所以我想添加一个加载屏幕,以便外部页面在显示之前完全加载。我无法控制外部网站,并且外部网站具有同源策略,因此我无法在 iFrame
我正在尝试为我的应用程序开发一个Dockerfile,该文件在初始化后加载大量环境变量。不知何故,当我稍后执行以下命令时,这些变量是不可用的: docker exec -it container_na
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我刚刚遇到一个问题,我有一个带有一些不同选项的选择标签。 现在我想检查用户选择了哪些选项。 然后我想将一个新的 html 文件加载到该网站(取决于用户选中的选项)宽度 javascript,我该怎么做
我知道两种保存/加载应用程序设置的方法: 使用PersistentStore 使用文件系统(存储,因为 SDCard 是可选的) 我想知道您使用应用程序设置的做法是什么? 使用 PersistentS
我开始使用 Vulkan 时偶然发现了我的第一个问题。尝试创建调试报告回调时(验证层和调试扩展在我的英特尔 hd vulkan 驱动程序上可用,至少它是这么说的),它没有告诉我 vkCreateDeb
我是一名优秀的程序员,十分优秀!