java在static静态方法中获取并使用yml配置文件的值
例如要获取yml文件的启动环境配置
spring:
profiles:
active: dev
使用配置文件读取
新建配置类
@Component
public class ErpEnvironmentConfig {
public static String profile;
@Value("${spring.profiles.active}")
public String profileActive;
/**
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。
PostConstruct在构造函数之后执行,init()方法之前执行
*/
@PostConstruct
private void getEnvironment() {
profile = this.profileActive;
}
}
在静态代码块直接调用
//spring自动注入
@Autowired
ErpEnvironmentConfig erpEnvironmentConfig;
public static void test()
//直接通过类名获取
String yourName= ErpEnvironmentConfig.profile;
}