简体   繁体   English

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

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

My spring application start's via WebApplicationInitializer.我的 spring 应用程序通过 WebApplicationInitializer 启动。

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

I have Config class我有配置类

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

And in application.properties file I try to override the spring and logging properties在 application.properties 文件中,我尝试覆盖 spring 和日志属性

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

The file application.properties is in the directory src/main/resources文件 application.properties 位于 src/main/resources 目录中

But it has no effect - means that no property is applied to the spring or logging system但它没有效果 - 意味着没有属性应用于弹簧或日志系统

Try using SpringBoot.尝试使用 SpringBoot。 In spring boot application.properties is read by default and you don't even need to use @PropertySource to read it.在 spring boot 中 application.properties 是默认读取的,您甚至不需要使用 @PropertySource 来读取它。 You can download a spring boot application skeleton from https://start.spring.io/您可以从https://start.spring.io/下载 Spring Boot 应用程序框架

In Spring boot, we can simply put an application.properties file in our src/main/resources directory, and it will be auto-detected.在 Spring boot 中,我们可以简单地将 application.properties 文件放在我们的 src/main/resources 目录中,它会被自动检测到。 We can then inject any loaded properties from it as normal.然后我们可以像往常一样从它注入任何加载的属性。

check for the location where you have placed your application.properties file.检查您放置 application.properties 文件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM