在spring boot项目中,@Value只能获取非静态变量,否则是null
/**
* cron="0 */1 * * * ?"
*/
@Value("${system.cron}")
private String cron;
/**
* cron1=null
*/
@Value("${system.cron}")
private static String cron1;
静态块获取application.yml文件中的数据,启动程序报null???
因为在加载静态变量时,该类还没有实例化,导致@Value无法赋值。
补充:(不常用)
@PostConstruct修饰的方式会在服务器加载servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
因为这些都是在构造函数之后执行,所以无法给静态代码块赋值。