简体   繁体   中英

How to load application.properties in spring framework?

My spring application start's via 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

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

The file application.properties is in the directory src/main/resources

But it has no effect - means that no property is applied to the spring or logging system

Try using SpringBoot. In spring boot application.properties is read by default and you don't even need to use @PropertySource to read it. You can download a spring boot application skeleton from https://start.spring.io/

In Spring boot, we can simply put an application.properties file in our src/main/resources directory, and it will be auto-detected. We can then inject any loaded properties from it as normal.

check for the location where you have placed your application.properties file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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