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


i have traditional war deployment of spring boot app tomcat 8. though have @propertysources annotation defined, properties defined in property file given in directory specified in @propertysources not seem found. modified spring boot web sample project , replicate problem. in example below, result of calling web controller "hello default, greetings spring boot!". never picks "name" .property external application.properties (if remove property entirely class properties file deployment fails because property never resolves). did miss such external properties not seem 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 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 shows property sources:

2015-06-14 22:02:11.685 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletconfiginitparams] propertysource lowest search precedence 2015-06-14 22:02:11.687 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:11.687 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:11.687 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:11.687 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:11.688 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment propertysources [servletconfiginitparams,servletcontextinitparams,jndiproperties,systemproperties,systemenvironment] 2015-06-14 22:02:11.717 debug 5010 --- [ost-startstop-1] o.s.boot.springapplication               : running 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 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 , finish default scanning 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' 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 [servletcontextinitparams] 2015-06-14 22:02:11.745 debug 5010 --- [ost-startstop-1] ationconfigembeddedwebapplicationcontext : bean factory 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 lowest search precedence 2015-06-14 22:02:12.269  info 5010 --- [ost-startstop-1] o.s.b.f.s.defaultlistablebeanfactory     : overriding bean definition 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]] [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 search precedence lower [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 locate messagesource 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 servletcontext attribute 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 lowest search precedence 2015-06-14 22:02:12.953 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:12.953 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:12.953 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:12.953 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:12.953 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment 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 native not problem. 2015-06-14 22:02:12.995 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletconfiginitparams] propertysource lowest search precedence 2015-06-14 22:02:12.995 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:12.995 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:12.995 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:12.995 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:12.995 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment 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 lowest search precedence 2015-06-14 22:02:13.010 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:13.010 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:13.010 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:13.010 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:13.010 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment 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 lowest search precedence 2015-06-14 22:02:13.046 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:13.046 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:13.046 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:13.046 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:13.046 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment 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 lowest search precedence 2015-06-14 22:02:13.074 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:13.074 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:13.074 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:13.074 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:13.075 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment 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 lowest search precedence 2015-06-14 22:02:13.101 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [servletcontextinitparams] propertysource lowest search precedence 2015-06-14 22:02:13.101 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [jndiproperties] propertysource lowest search precedence 2015-06-14 22:02:13.101 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemproperties] propertysource lowest search precedence 2015-06-14 22:02:13.101 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : adding [systemenvironment] propertysource lowest search precedence 2015-06-14 22:02:13.101 debug 5010 --- [ost-startstop-1] o.s.w.c.s.standardservletenvironment     : initialized standardservletenvironment propertysources [servletconfiginitparams,servletcontextinitparams,jndiproperties,systemproperties,systemenvironment] 

when using traditional tomcat war deployment, recommend using folder path relative classpath, instead of absolute path.

you need define variable spring.config.location @ beginning when spring boot application 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 having same problem , found solution here: how externalize spring boot application.properties tomcat/lib folder


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -