Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@PropertySource not picked up in a spring boot application using traditional tomcat war deployment

I have a traditional war deployment of a spring boot app to tomcat 8. Even though I have the @PropertySources annotation defined, the properties defined in the property file given in the directory specified in the @PropertySources do not seem to be found. I modified a spring boot web sample project and could replicate the problem. In the example below, the result of calling the web controller is always "Hello Default, Greetings from Spring Boot!". It never picks up the "name" .property from the external application.properties (If I remove the property entirely from the class properties file the deployment fails because the property never resolves). What did I miss such that the external properties does not seem to get picked up?

My Application class:

@SpringBootApplication
@Profile("dev")
@PropertySource(value = "file:/usr/local/tomcat/config/")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        return builder.profiles("dev").sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Web controller:

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @RequestMapping("/")
    public String index() {
        return "Hello " +name+ ", Greetings from Spring Boot!";
    }
}

src/main/resources/application.properties:

debug=true
logging.level.org.springframework.boot.context=DEBUG
logging.level.org.springframework.boot.env=DEBUG
logging.level.org.springframework.web=DEBUG
name=Default

external application.properties in /usr/local/tomcat/config

name=Default

catalina.out section that shows the property sources:

2015-06-14 22:02:11.685 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:11.688 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:11.717 DEBUG 5010 --- [ost-startStop-1] o.s.boot.SpringApplication               : Running with Spring Boot v1.2.2.RELEASE, Spring v4.1.5.RELEASE
2015-06-14 22:02:11.718 DEBUG 5010 --- [ost-startStop-1] o.s.boot.SpringApplication               : Loading source class hello.Application,class org.springframework.boot.context.web.ErrorPageFilter
2015-06-14 22:02:11.737 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application.xml' resource not found
[typical scanning for properties files]
2015-06-14 22:02:11.738 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'classpath:/application.properties' 
[continue and finish default scanning for properties files]

2015-06-14 22:02:11.738 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application.yaml' for profile devresource not found
2015-06-14 22:02:11.742  INFO 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31c0c0c3: startup date [Sun Jun 14 22:02:11 EDT 2015]; root of context hierarchy
2015-06-14 22:02:11.745 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2015-06-14 22:02:11.745 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31c0c0c3: org.springframework.beans.factory.support.DefaultListableBeanFactory@1319bea3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,application,errorPageFilter]; root of factory hierarchy
2015-06-14 22:02:11.786 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [URL [file:/usr/local/apache-tomcat-8.0.23/config/]] PropertySource with lowest search precedence
2015-06-14 22:02:12.269  INFO 5010 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [applicationConfig: [classpath:/application.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties]
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Removing [applicationConfigurationProperties] PropertySource
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Removing [defaultProperties] PropertySource
2015-06-14 22:02:12.641 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@3fd3a17b]
2015-06-14 22:02:12.642 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Using ApplicationEventMulticaster [org.springframework.context.event.SimpleApplicationEventMulticaster@2c2545cb]
2015-06-14 22:02:12.644 DEBUG 5010 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2015-06-14 22:02:12.644  INFO 5010 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 902 ms
2015-06-14 22:02:12.943 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.e.ServletContextInitializerBeans : Added existing Servlet initializer bean 'dispatcherServletRegistration'; order=2147483647, resource=class path resource [org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class]
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:12.984  INFO 5010 --- [ost-startStop-1] b.a.w.TomcatWebSocketContainerCustomizer : NonEmbeddedServletContainerFactory detected. Websockets support should be native so this normally is not a problem.
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.075 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.100 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
like image 658
cris Avatar asked Jun 15 '15 02:06

cris


People also ask

What is @PropertySource in spring?

Spring @PropertySource annotation is used to provide properties file to Spring Environment. This annotation is used with @Configuration classes. Spring PropertySource annotation is repeatable, means you can have multiple PropertySource on a Configuration class.

Can we deploy spring boot application in Tomcat?

By using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.

Where do I put application properties in Tomcat?

properties outside the classpath or the tomcat container, like d:\application.

How does Tomcat work with Spring boot?

Spring Boot has a complete Tomcat inside. It builds a so-called fat-jar with everything needed inside. You don't need Tomcat installed in your system. BTW: Spring Boot also supports other application servers like Jetty.


1 Answers

When using traditional tomcat war deployment, I would recommend using a folder path relative to the classpath, instead of an absolute path.

You will need to define the variable spring.config.location at the beginning when Spring Boot application is configured:

public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
        return springApplicationBuilder
                .sources(Application.class)
                .properties(getProperties());
    }

    public static void main(String[] args) {

        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
                .sources(Application.class)
                .properties(getProperties())
                .run(args);
    }

   static Properties getProperties() {
      Properties props = new Properties();
      props.put("spring.config.location", "classpath:myapp1/");
      return props;
   }

I was having the same problem and found a solution here: How to externalize Spring Boot application.properties to tomcat/lib folder

like image 168
Daniel Mora Avatar answered Sep 25 '22 13:09

Daniel Mora