Spring boot 中主动读取Application.yml 配置文件

原因: 因为配置中心需要统一配置文件原因,未方便后续统一配置变更,现在将redis.properties 中配置迁移到Application.yml 配置中

原来配置:

原来读取配置代码:

// 构建缓存客户端

private static JedisCluster cluster;

private static final GsonBuilderbuilder = new GsonBuilder();

static {

initialShardedPool();

}

private static voidinitialShardedPool() {

int DEFAULT_TIMEOUT = 2000;连接超时

int DEFAULT_REDIRECTIONS = 5;//重试次数

JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig();

Set hosts = newHashSet();

// 设置缓存服务器地址,可以设置多个实现分布式缓存

String cachedPath =PropertiesUtil.getProperty("redis.properties","redis.cluster.nodes").trim();

String[] servers = cachedPath.split(",");

if (servers.length > 0) {

for (int i = 0; i < servers.length; i++) {

String[] path = servers[i].split(":");

HostAndPort hp = new HostAndPort(path[0],Integer.parseInt(path[1]));

hosts.add(hp);

}

}

DEFAULT_CONFIG.setMaxTotal(Integer.parseInt(PropertiesUtil.getProperty("redis.properties","jedis.pool.maxTotal")));

DEFAULT_CONFIG.setMaxIdle(Integer.parseInt(PropertiesUtil.getProperty("redis.properties","jedis.pool.maxIdle")));

DEFAULT_CONFIG.setMinIdle(Integer.parseInt(PropertiesUtil.getProperty("redis.properties","jedis.pool.minIdle")));

DEFAULT_CONFIG.setMaxWaitMillis(Integer.parseInt(PropertiesUtil.getProperty("redis.properties","jedis.pool.maxWaitMillis")));

String dpass =PropertiesUtil.getProperty("redis.properties","redis.key").trim();

try {

//                        

cluster = new JedisCluster(hosts, DEFAULT_TIMEOUT, //连接超时

//                                        

        DEFAULT_TIMEOUT,//读写超时

//                        DEFAULT_REDIRECTIONS, DEFAULT_CONFIG);

 cluster = newJedisCluster(hosts, DEFAULT_TIMEOUT, //连接超时

 DEFAULT_TIMEOUT,//读写超时

 DEFAULT_REDIRECTIONS, dpass,DEFAULT_CONFIG);

//                        cluster= new JedisCluster(hosts);

} catch (Exception e) {

e.printStackTrace();

}

}

修改后Application.yml增加jedis 属性数据:

创建javabean,来专门映射配置的话,我们一般会使用@ConfigurationProperties来读取.

packagecom.sitech.pgcent.util;

importorg.springframework.boot.context.properties.ConfigurationProperties;

importorg.springframework.stereotype.Component;

/**

*@Projectpgcent-project

*@Packagecom.sitech.pgcent.util

*@ClassNameJedisApplicationUntil

*@DescripitionTODO

*@Authorliuzk

*@Date2020/3/310:36

*@Version1.0

**/

@Component

//接收application.yml中的wechat下面的属性

@ConfigurationProperties(prefix="jedis")

publicclassJedisApplicationUntil{

privateStringmaxTotal;

privateStringmaxIdle;

privateStringminIdle;

privateStringmaxWaitMillis;

privateStringkey;

privateStringnodes;

publicStringgetMaxTotal(){

returnmaxTotal;

}

publicvoidsetMaxTotal(StringmaxTotal){

this.maxTotal=maxTotal;

}

publicStringgetMaxIdle(){

returnmaxIdle;

}

publicvoidsetMaxIdle(StringmaxIdle){

this.maxIdle=maxIdle;

}

publicStringgetMinIdle(){

returnminIdle;

}

publicvoidsetMinIdle(StringminIdle){

this.minIdle=minIdle;

}

publicStringgetMaxWaitMillis(){

returnmaxWaitMillis;

}

publicvoidsetMaxWaitMillis(StringmaxWaitMillis){

this.maxWaitMillis=maxWaitMillis;

}

publicStringgetKey(){

returnkey;

}

publicvoidsetKey(Stringkey){

this.key=key;

}

publicStringgetNodes(){

returnnodes;

}

publicvoidsetNodes(Stringnodes){

this.nodes=nodes;

}

}

当static 语句块中调用initialShardedPool() 方法报错:

提示信息是

查看发现报错的原因是在执行静态语句块的时候,@Autowired 还未执行,

Java变量的初始化顺序为:静态变量或静态语句块–>实例变量或初始化语句块–>构造方法–>@Autowired

所有会报空指针异常。

因为必须使用静态语句块读取配置,所有,只能放弃使用spring boot 容器的注解,改成主动读取配置文件。

1.添加依赖

2.代码修改

static{

initialShardedPool();

}

privatestaticvoidinitialShardedPool(){

intDEFAULT_TIMEOUT=200000;

intDEFAULT_REDIRECTIONS=5;

JedisPoolConfigDEFAULT_CONFIG=newJedisPoolConfig();

Set<HostAndPort>hosts=newHashSet<HostAndPort>();

//设置缓存服务器地址,可以设置多个实现分布式缓存

try{

Yamlyaml=newYaml();

URLurl=CacheUtil.class.getClassLoader().getResource("application.yml");

if(url!=null){

Mapmap=yaml.load(newFileInputStream(url.getFile()));

MapjedisMap=(Map)map.get("jedis");

StringcachedPath=(String)jedisMap.get("nodes");

String[]servers=cachedPath.split(",");

if(servers.length>0){

for(inti=0;i<servers.length;i++){

String[]path=servers[i].split(":");

HostAndPorthp=newHostAndPort(path[0],Integer.parseInt(path[1]));

hosts.add(hp);

}

}

DEFAULT_CONFIG

.setMaxTotal(Integer.parseInt(String.valueOf(jedisMap.get("maxTotal"))));

DEFAULT_CONFIG

.setMaxIdle(Integer.parseInt(String.valueOf(jedisMap.get("maxIdle"))));

DEFAULT_CONFIG

.setMinIdle(Integer.parseInt(String.valueOf(jedisMap.get("minIdle"))));

DEFAULT_CONFIG.setMaxWaitMillis(

Integer.parseInt(String.valueOf(jedisMap.get("maxWaitMillis"))));

Stringdpass=(String)jedisMap.get("key");

cluster=newJedisCluster(hosts,DEFAULT_TIMEOUT,DEFAULT_TIMEOUT,

DEFAULT_REDIRECTIONS,dpass,DEFAULT_CONFIG);

//cluster=newJedisCluster(hosts,DEFAULT_TIMEOUT,DEFAULT_TIMEOUT,

//DEFAULT_REDIRECTIONS,DEFAULT_CONFIG);

//cluster=newJedisCluster(hosts);

}

}catch(Exceptione){

e.printStackTrace();

}

}

3.测试成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_41007138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值