“不能静态地引用非静态字段”。设置变量到System.SetProperty时出错

发布于 2025-01-21 11:29:08 字数 593 浏览 1 评论 0原文

我正在尝试将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2025-01-28 11:29:08

主要方法是一种静态方法,它被调用没有类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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文