Spring Boot读取yml或者properties配置数据
程序员文章站
2022-06-28 18:15:43
1 使用@Value注解一般用于 非static@Value 注解即可获取。增加注解@RefreshScope,可以使得配置实时生效(实践使用nacos做配置中心的时候)@Configuration@RefreshScopepublic class InquiryConfig { @Value("${prepared.template.filepath}") private String templateFilePath;} static也可以使用@Value,使用...
1 使用@Value注解
一般用于 非static
@Value 注解即可获取。
增加注解@RefreshScope
,可以使得配置实时生效(实践使用nacos做配置中心的时候)
@Configuration
@RefreshScope
public class InquiryConfig {
@Value("${prepared.template.filepath}")
private String templateFilePath;
}
static也可以使用@Value
,使用setter方法.
public static String tempFileName;
private static String templateFilePath;
@Value("${esign.templateFile}")
public void setTxtResource(String templateFilePath) {
tempFileName = templateFilePath;
}
2使用Environment
可以获取static
修饰的变量
@Autowired
private Environment env;
public static String hbase_zookeeper_quorum;
public static String hbase_zookeeper_property_clientPort;
@PostConstruct
private void init() {
hbase_zookeeper_quorum = env.getProperty("hbase.quorum");
hbase_zookeeper_property_clientPort = env.getProperty("hbase.clientPort");
conf = HBaseConfiguration.create();
System.out.println(hbase_zookeeper_quorum);
System.out.println(".>>>>>>>>>>>>>>>>>");
conf.set("hbase.zookeeper.quorum", hbase_zookeeper_quorum);
conf.set("hbase.zookeeper.property.clientPort", hbase_zookeeper_property_clientPort);
try {
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
- 增加 Environment
- PostConstuct 注解方法,第一次执行
- env.getProperty(“hbase.quorum”) 获取具体值
3 读取文件的方式
读取config.preperties
文件的所有配置
使用方式:
SysConfig.getInstance().getProperty("属性key");
// 比如
SysConfig.getInstance().getProperty("message.templateid");
代码
package com.prepared.config;
import org.apache.commons.lang.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;
public class SysConfig {
private Properties props = null;// config.properties
private static volatile SysConfig conf;
private SysConfig() {
props = new Properties();
loadConfigProps();
}
public static SysConfig getInstance() {
if (conf == null) {
synchronized (SysConfig.class) {
if (conf == null) {
conf = new SysConfig();
}
}
}
return conf;
}
public void loadConfigProps() {
InputStream is = null;
try {
is = getClass().getResourceAsStream("/WEB-INF/classes/config.properties");
if (is == null) {
is = getClass().getResourceAsStream("/config.properties");
}
InputStreamReader reader = new InputStreamReader(is, "UTF-8");
props.load(reader);
Iterator<String> iter = props.stringPropertyNames().iterator();
while (iter.hasNext()) {
String key = iter.next();
props.setProperty(key, props.getProperty(key));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
is = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public String getProperty(String key) {
String tmp = props.getProperty(key);
if (StringUtils.isNotEmpty(tmp)) {
return tmp.trim();
}
return tmp;
}
public String getProperty(String key, String defaultValue) {
String tmp = props.getProperty(key, defaultValue);
if (StringUtils.isNotEmpty(tmp)) {
return tmp.trim();
}
return tmp;
}
public int getPropertyInt(String key) {
String tmp = props.getProperty(key);
if (StringUtils.isNotEmpty(tmp)) {
return Integer.parseInt(tmp.trim());
}
return 0;
}
public int getPropertyInt(String key, int defaultValue) {
String tmp = props.getProperty(key);
if (StringUtils.isNotEmpty(tmp)) {
return Integer.parseInt(tmp.trim());
}
return defaultValue;
}
public long getPropertyLong(String key, long defaultValue) {
String tmp = props.getProperty(key);
if (StringUtils.isNotEmpty(tmp)) {
return Integer.parseInt(tmp.trim());
}
return defaultValue;
}
}
本文地址:https://blog.csdn.net/Prepared/article/details/110947516
推荐阅读
-
spring-boot读取props和yml配置文件的方法
-
spring boot配置文件application.properties配置JPA以及数据源
-
Spring Boot读取yml或者properties配置数据
-
spring-boot读取props和yml配置文件
-
spring-boot读取props和yml配置文件
-
Spring Boot——读取.properties配置文件解决方案
-
Spring Boot ApplicationListener监听器中读取properties配置项
-
Spring boot 读取配置文件(application.yml)中的属性值
-
spring boot 读取配置文件(application.yml)中的属性值
-
Spring boot 读取配置文件(application.yml)中的属性值