static静态方法使用yml配置文件的值
package com.longfor.falcon.modules.sys.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Date 2020/8/28——10:22
* @ClassName ReadYmlStaticParUtil
* @Description: static静态方法使用yml配置文件的值
* @Author hph
**/
@Component
public class ReadYmlStaticParUtil {
/*
* @date 2020/8/28 ——10:28
* @author hph
* 注意事项:
* 首选要在util里建一个static的变量,然后使用@Value获取yml配置文件的值。
* 最后关键就是使用 @PostConstruct 熟悉将yml中配置的值赋给本地的变量,
* 这样后面的静态方法就能使用了。
* 被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会
* 被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
* PreDestroy()方法在destroy()方法执行执行之后执行。
* 注意util类使用了 @Component 属性注解了说明是需要在启动类 Application
* 启动的时候加载的,所以我们本地写一个方法调用 util 的时候是获取不到 name的。
* @param
* @return
*/
//定义一个yml配置文件里的静态变量
private static String userNames;
//通过value获取yml配置文件参数
@Value("${falcon.external.whiteList.userName}")
private String userName;
//利用@PostConstruct将yml中配置的值赋给本地的变量
@PostConstruct
public void getUserName(){
userNames = this.userName;
}
public static String getName(){
//输出打印控制台
System.out.println(userNames);
return userNames;
}
}
还没有评论,来说两句吧...