Home > database >  How to load application.properties in spring framework?
How to load application.properties in spring framework?

Time:10-26

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

CodePudding user response:

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/

CodePudding user response:

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.

  • Related