Spring Boot 创建全局变量

一、定义参数

在application.properties中第一自己想要定义的参数。

ziweidajiu.redisPath = 127.0.0.1

二、定义一个Model

自定义一个model接受配置文件中的参数

@ConfigurationProperties(prefix = "ziweidajiu")
public class Ziweidajiu {

    private String redisPath;

    public String getRedisPath() {
        return redisPath;
    }

    public void setRedisPath(String redisPath) {
        this.redisPath = redisPath;
    }
}

三、在SpringBootApplication或使用的类中加入

@EnableConfigurationProperties({Ziweidajiu.class})

四、使用

@Autowired
private Ziweidajiu ziweidajiu;

Jedis jedis = new Jedis(ziweidajiu.getRedisPath(), 6379);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容