“不能静态地引用非静态字段”。设置变量到System.SetProperty时出错
我正在尝试将String
变量分配给从主方法中从application.properties
中获取的系统属性。但这给了我
无法静态引用非静态字段log_file
这是我的代码段,我在这里犯的错误是什么?
@SpringBootApplication
public class MqMessageHandlerApplication {
@Value("${logging.home.file}")
String LOG_FILE;
public static void main(String[] args) {
//System.setProperty("LOG_DIR", "D:\\mq-message-handler-1.0\\logs\\" );
System.setProperty("LOG_DIR", LOG_FILE );
SpringApplication.run(MqMessageHandlerApplication.class, args);
}
}
I'm trying to assign a String
variable to a system property fetched from the application.properties
in main method. But it gives me
Cannot make a static reference to the non-static field LOG_FILE
This is my code snippet, what is the mistake I'm making here?
@SpringBootApplication
public class MqMessageHandlerApplication {
@Value("${logging.home.file}")
String LOG_FILE;
public static void main(String[] args) {
//System.setProperty("LOG_DIR", "D:\\mq-message-handler-1.0\\logs\\" );
System.setProperty("LOG_DIR", LOG_FILE );
SpringApplication.run(MqMessageHandlerApplication.class, args);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要方法是一种静态方法,它被调用没有类MqmessageHandlerApplication的对象进行实例化,这就是说,您无法调用log_file,只有在实例化mqmessageHandlerApplication的对象时,它才能访问。尝试在主体内部获取此文件,而不是使用现场注入,然后将其起作用。
The main method is a static method, it is called without a object of your class MqMessageHandlerApplication being instantiated, this being said, you can't call the LOG_FILE, it will only be accessible when you instantiate a object of MqMessageHandlerApplication. Try to fetch this file inside the main body, not using a field injection, then it will work.