- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章使用springboot在工具类中读取配置文件(ClassPathResource)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1
2
3
4
5
|
spring.activemq.broker-url=tcp:
//localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=
true
spring.activemq.pool.enabled=
false
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
com.jeff.utils;
import
java.io.IOException;
import
java.util.Properties;
import
org.springframework.core.io.ClassPathResource;
import
org.springframework.core.io.support.PropertiesLoaderUtils;
public
class
PropertiesUtil {
private
static
String user;
static
{
System.out.println(
"application.properties属性文件读取开始"
);
ClassPathResource resource =
new
ClassPathResource(
"application.properties"
);
try
{
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
user = properties.getProperty(
"spring.activemq.user"
);
System.out.println(
"user的值:"
+ user);
}
catch
(IOException e) {
System.out.println(
"application.properties属性文件读取异常"
+ e);
}
System.out.println(
"application.properties属性文件读取完成"
);
}
public
static
String getUser() {
System.out.println(
"获取user的值:"
+ user);
return
user;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.jeff.controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController;
import
com.jeff.utils.PropertiesUtil;
@RestController
public
class
MyController {
@RequestMapping
(
"myTest"
)
public
String myTest() {
PropertiesUtil.getUser();
return
"success"
;
}
}
|
通常我们读取配置文件可以用@Value注解和@Configuration,@ConfigurationProperties(prefix = "xxx")等注解,但是这种方式是无法把配置读取到静态变量的,如果我们想在项目初始化时把配置文件加载到一个工具类,然后通过静态变量的方式调用的话我们就不能使用这两种方法.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
javax.annotation.PostConstruct;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.core.env.Environment;
import
org.springframework.stereotype.Component;
/**
*
* @Description: 配置常量类——根据不同的spring-profile加载不同的配置
* @author: eric.zhang
* @date: 2018年7月20日 上午10:59:24
*/
@Component
public
class
ConfigConstant {
@Autowired
private
Environment env;
public
static
String url;
public
static
String param;
@PostConstruct
public
void
readConfig() {
url = env.getProperty(
"config.url"
);
param = env.getProperty(
"config.param"
);
}
}
|
我写完以后发现有些麻烦,下面是改进的方法,不需要每个配置都去get一下,只需要把配置文件的key与工具类的静态变量名写成一样的即可.
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
|
import
java.io.UnsupportedEncodingException;
import
java.lang.reflect.Field;
import
javax.annotation.PostConstruct;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.core.env.Environment;
import
org.springframework.stereotype.Component;
/**
*
* @Description: 配置常量类——根据不同的spring-profile加载不同的配置,变量名要与配置文件里写的名一致
* @author: eric.zhang
* @date: 2018年7月20日 上午10:59:24
*/
@Component
public
class
ConfigConstant {
@Autowired
private
Environment env;
public
static
String url;
public
static
String name;
@PostConstruct
public
void
readConfig()
throws
Exception {
String prefix =
"config."
;
Field[] fields = ConfigConstant.
class
.getFields();
for
(Field field : fields ){
field.set(
null
, getProperty(prefix + field.getName()));
}
}
private
String getProperty(String key)
throws
UnsupportedEncodingException {
return
new
String(env.getProperty(key).getBytes(
"ISO-8859-1"
),
"UTF-8"
);
}
}
|
大哥说这样写依赖spring, 单测调代码的时候不方便,所以又写了一个不依赖spring的版本.
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
|
import
java.io.InputStreamReader;
import
java.lang.reflect.Field;
import
java.util.Properties;
/**
*
* @Description: 配置常量类——根据不同的spring-profile加载不同的配置
* 变量名把配置文件的key中的"."替换成"_"命名
* @author: eric.zhang
* @date: 2018年7月20日 上午10:59:24
*/
public
class
ConfigConstant {
public
static
String CONFIG_URL;
public
static
String CONFIG_NAME;
static
{
try
{
Properties props =
new
Properties();
props.load(
new
InputStreamReader(
ConfigConstant.
class
.getClassLoader().getResourceAsStream(
"application.properties"
),
"UTF-8"
));
String profile = props.getProperty(
"spring.profiles.active"
);
String envFile =
"application-"
+ profile +
".properties"
;
Properties envProps =
new
Properties();
envProps.load(
new
InputStreamReader(
ConfigConstant.
class
.getClassLoader().getResourceAsStream(envFile),
"UTF-8"
));
Field[] fields = ConfigConstant.
class
.getFields();
for
(Field field : fields) {
field.set(
null
, envProps.getProperty(field.getName().replace(
"_"
,
"."
).toLowerCase()));
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/weixin_45739720/article/details/103701627 。
最后此篇关于使用springboot在工具类中读取配置文件(ClassPathResource)的文章就讲到这里了,如果你想了解更多关于使用springboot在工具类中读取配置文件(ClassPathResource)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我需要安装一个 VPN 配置文件,以后可以在没有用户干预的情况下进行更改。直接的方法是安装 VPN 配置文件,然后安装 MDM 配置文件,以便以后可以更改配置。 但是,我的要求仅与 VPN 配置有关,
配置文件 全局配置文件hibernate.cfg.xml hibernate.cfg.xml --> com.mysql.cj
我想知道有没有一种方法可以设置配置,我可以有两个数据库条目,当环境是本地时和在服务器上时都可以工作。 我很久以前就通过代码进行切换时遇到了一个解决方案。现在无法找到它。你们是怎么做到的? 最佳答案 我
通常我总能在 Stack Overflow 上找到我要找的东西。但是这次我真的难住了。 我已经阅读了针对遇到类似问题的人的其他 Stack Overflow 回复;但无济于事。 (我认为其中一部分原因
我是 Hadoop 新手。我正在尝试设置单节点集群。 我注意到在我读过的文档中(甚至在 Apache 的配置站点上)它总是引用 conf/目录中的配置文件。但是,当我下载 2.X.X 版时,我只能在
好吧,我之前发布了如何读取其他程序的其他配置文件(这里是链接 Previous Post 。我设法做到了。但是现在还有另一个问题。场景是这样的,我有两个程序。程序 A 从配置文件中读取其配置,程序 B
【全局(系统)配置文件】 复制代码 代码如下: /etc/crontab Linux 主要的配置文件都在 etc 目录下。 复制代码
Apache HBase配置文件 本节是本章内容的开篇,我们首先来认识Apache HBase中有哪些需要的配置文件! Apache HBase使用与Apache Hadoop相同的配置系统。所有
我想了解如何为我们的实验构建配置文件 让我们以 AllenNLP 文档中的这个例子为例 training and prediction 特别是这个片段 我们从哪里得到“token_embedders”
我正在研究 MAVEN 配置文件,我有一个关于使用变量设置属性的问题。目前,我正在使用以下配置: Action type D restriction actionTy
我知道以前可能有人问过这个问题,但我似乎无法为我找到正确的答案。 我有一个名为 的 Windows 服务foobar.exe .我有一个名为 的应用程序配置文件foobar.exe.confi
我使用 FitNesse 作为功能测试框架。当 FitNesse 运行需要配置的代码时,我遇到了麻烦。 如果我需要从配置文件中获取连接字符串,我可以通过将它添加到 FitServer.exe.conf
我对我的 Jenkins 输出感到有些困惑。 关于 Jenkins 的工作:(在底部缩短了 pom.xml) mvn deploy -Pprofile1 我所有的插件将运行 4 次: 父/pom.xm
我偶尔会遇到一个问题,即为设备配置开发版本的应用程序。错误消息通常是“找不到此可执行文件的有效配置文件”。 我已遵循所有 Apple 说明:我拥有有效的开发证书,开发人员配置文件包含相关设备的设备 I
我正在尝试在 F# 控制台应用程序中使用 NLog,我已设法使用 App.config 中的配置部分使其工作,但无法使用独立的 NLog.config 文件使其工作。我的 NLog.config 文件
有没有办法像在 rubocop 中一样使用配置文件禁用 puppet-lint 中的检查?配置文件应该是txt文件、json文件还是其他格式? 最佳答案 是的,该文件名为 .puppet-lint.r
我有多个网站,它们都具有相同的代码,但应用程序设置不同。我想将我的应用程序设置放在位于虚拟目录中的单独配置文件中。这将允许我在所有站点之间共享所有代码的单个副本,每个站点具有不同的虚拟目录。 不幸的是
我在 Ubuntu 上使用 ghci 6.8.2。 ghci 是否使用配置文件来进行一些初始设置?例如::设置提示符“ghci>”。 最佳答案 您是否尝试过将 :set Prompt "ghci> "
我继承了一个运行 JBoss 7.x 服务器、java 后端等单个实例的项目。我对 JBoss 完全陌生,我对文件结构的配置感到好奇),我必须拥有什么以及在哪里。 documentation其结构与我
我有一个关于 Android 的问题。我只需要在开始时运行其中一项 Activity 一次。因此,通常最好的解决方案是创建包含标志 isFirstRun 的文件并在应用程序启动后检查该值。 但在我的应
我是一名优秀的程序员,十分优秀!