簡體   English   中英

如何在 spring 框架中加載 application.properties?

[英]How to load application.properties in spring framework?

我的 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.level.com.app=DEBUG
logging.level.org.springframework.web=DEBUG

文件 application.properties 位於 src/main/resources 目錄中

但它沒有效果 - 意味着沒有屬性應用於彈簧或日志系統

嘗試使用 SpringBoot。 在 spring boot 中 application.properties 是默認讀取的,您甚至不需要使用 @PropertySource 來讀取它。 您可以從https://start.spring.io/下載 Spring Boot 應用程序框架

在 Spring boot 中,我們可以簡單地將 application.properties 文件放在我們的 src/main/resources 目錄中,它會被自動檢測到。 然后我們可以像往常一樣從它注入任何加載的屬性。

檢查您放置 application.properties 文件的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM