【问题标题】:How to load application.properties in spring framework?如何在spring框架中加载application.properties?
【发布时间】:2021-12-10 21:58:40
【问题描述】:

我的 spring 应用程序是通过 WebApplicationInitializer 启动的。

public class WebAppInitializer implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException
    {
       ....
    }
}

我有配置类

@Configuration
@PropertySources({
    @PropertySource(value = "classpath:application.properties"))
})
public class MainConfig
{
  ....
}

在 application.properties 文件中,我尝试覆盖 spring 和 logging 属性

logging.level.com.app=DEBUG
logging.level.org.springframework.web=DEBUG

文件application.properties在目录src/main/resources中

但它没有任何作用——意味着没有属性被应用到spring或日志系统

【问题讨论】:

  • 没有效果是什么意思?你期待什么?
  • 有了您的@Configurationapplication.properties 就成为了财产来源。您还没有向我们展示您使用这些属性的任何地方。
  • 目前还不清楚你在问什么。为什么您希望您的财产来源适用于“日志系统”??
  • 为什么不呢?在所有的例子中,这都是写的。另外,弹簧本身的属性也没有应用
  • 什么例子? 弹簧本身的属性是什么意思?

标签: java spring properties property-files


【解决方案1】:

尝试使用 SpringBoot。在 spring boot application.properties 是默认读取的,你甚至不需要使用 @PropertySource 来读取它。可以从https://start.spring.io/下载一个spring boot应用骨架

【讨论】:

    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 2016-03-04
    • 2020-09-07
    • 1970-01-01
    • 2022-11-15
    • 2013-02-22
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多