springboot中static静态工具方法获取配置文件属性值

springBoot中static静态工具方法获取配置文件属性值

参考网址:

https://mp.weixin.qq.com/s/iOWdDnPt5aQaHR9gRjwtAA

http://www.shaoming.club/archives/springboot%E5%90%AF%E5%8A%A8%E6%97%B6%E8%AE%A9%E6%96%B9%E6%B3%95%E8%87%AA%E5%8A%A8%E6%89%A7%E8%A1%8C%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8Fmd

需求

定义全局常量

旧的方案

在项目中定义一个常量类或者常量接口

public class AppConstant {

    public static final String key = "f165bad5-c0da-43f9-a983-b86af1e0c266";

}

新的方案1

写了一个读取配置文件的类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/*
说明:
使用@Configuration和@Component注解都是可以的
 */
//@Configuration
@Component
public class QiNiuCloudConfiguration {

    public static String accessKey;
    @Value("${qinNiuCloud.ACCESS_KEY}")
    private String tempAccessKey;
    // 该注解是关键
    @PostConstruct
    public void setAccessKey() {
        accessKey = this.tempAccessKey;
    }
}

在application.properties添加属性配置

qinNiuCloud.ACCESS_KEY=f165bad5-c0da-43f9-a983-b86af1e0c266

新的方案2

写了一个读取配置文件的类

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
说明:
使用@Configuration和@Component注解都是可以的
 */
//@Configuration
@Component
public class QiniuyunStaticConfiguration implements InitializingBean {
    public static String ACCESS_KEY;
    @Value("${qinNiuCloud.ACCESS_KEY}")
    private String accessKey;
    @Override
    public void afterPropertiesSet() throws Exception {
       ACCESS_KEY = accessKey;
    }
}

测试

三种方式定义项目中常量

建议使用新的方案1,2,因为这两种方式比较灵活,可以在配置文件中指定项目常量

在启动类中测试,获取常量

import com.example.demo.constant.AppConstant;
import com.example.demo.constant.QiNiuCloudConfiguration;
import com.example.demo.constant.QiniuyunStaticConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        System.out.println("定义全局常量类获取常量,常量的值为:  "+AppConstant.APP_KEY);
        System.out.println("定义读取配置文件属性值的类,使用@PostConcust注解的方式,常量的值为:  "+QiNiuCloudConfiguration.accessKey);
        System.out.println("定义读取配置文件属性值的类,使用实现InitializingBean接口的方式,常量的值为:  "+QiniuyunStaticConfiguration.ACCESS_KEY);

    }

}

控制台打印

f165bad5-c0da-43f9-a983-b86af1e0c266
f165bad5-c0da-43f9-a983-b86af1e0c266
f165bad5-c0da-43f9-a983-b86af1e0c266

个人csdn博客网址:https://blog.csdn.net/shaoming314

jam

个人博客网址:www.shaoming.club

halo

个人gitee地址:https://gitee.com/shao_ming314/note

111

勇敢牛牛,不怕困难

存中…(img-tMnT6pne-1628736274481)]

个人gitee地址:https://gitee.com/shao_ming314/note

[外链图片转存中…(img-J9r9WxRh-1628736274484)]

勇敢牛牛,不怕困难

img

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot,要从Nacos配置获取,可以使用阿里巴巴的nacos-spring-boot-starter组件来实现。首先,在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>nacos-config</artifactId> </dependency> ``` 然后,在需要获取配置的静态方法,可以使用以下代码来获取Nacos配置: ```java import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; @Component @RefreshScope public class MyUtil { @Autowired private ConfigService configService; public static String getConfig(String dataId, String group, long timeout) throws NacosException { return SpringContextUtil.getBean(ConfigService.class).getConfig(dataId, group, timeout); } } ``` 在上面的代码,我们通过@Autowired注解注入了ConfigService,然后在静态方法通过SpringContextUtil.getBean(ConfigService.class)获取ConfigService实例,然后调用getConfig方法获取配置。同时,我们在MyUtil类上使用了@RefreshScope注解,表示当Nacos配置心的配置发生变化时,该类的Bean也会随之刷新。 需要注意的是,使用@RefreshScope注解需要在SpringBoot配置文件添加以下配置: ```yaml spring: cloud: nacos: config: refreshable-dataids: your-data-id ``` 其your-data-id为你需要动态刷新的配置项的dataId。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值