java - Why does Spring's PropertySource throw a FileNotFoundException? -


i trying load properties file using spring, located in web-inf folder.

i receiving following error:

org.springframework.beans.factory.beandefinitionstoreexception: failed parse configuration class [com.elastictest.config.prodconfig]; nested exception java.io.filenotfoundexception: \web-inf\config\prod.properties (the system cannot find path specified)

here production configuration file:

@configuration @profile("prod") @propertysource("file:${prodpropertiesfile}") public class prodconfig {      @bean     public static propertysourcesplaceholderconfigurer propertysourcesplaceholderconfigurer() {         return new propertysourcesplaceholderconfigurer();     }  } 

here web.xml declaration:

<context-param>     <param-name>prodpropertiesfile</param-name>     <param-value>/web-inf/config/prod.properties</param-value> </context-param> 

i have opened war , verified properties file there under web-inf/config folder. ideas?

the locations specify within @propertysource annotation processed resourceloader. in case, annotationconfigwebapplicationcontext, since web application , want use annotation configuration.

as resourceloader implementation, annotationconfigwebapplicationcontext knows how resolve location values find , initialize resource objects. uses these create resourcepropertysource objects used in property resolution.

the location value resolution follows few steps:

  • if starts /, it's interpreted servlet context resource , spring attempts retrieve through servletcontext#getresourceasstream.
  • if starts classpath:, it's interpreted classpath resource , spring tries retrieve through classloader#getresourceasstream, given appropriate classloader.
  • otherwise, defers resolution urlresource uses url locate resource. javadoc states

    supports resolution url , file in case of "file:" protocol.

in case, prefixed location file: spring tried use urlresource. path had leading / file system consider beginning of absolute path. don't have file @ location, /web-inf/config/prod.properties.

if use classpath: prefix suggested in this answer, must make sure /web-inf/config/prod.properties on classpath. that's uncommon thing put on classpath, don't recommend it.

finally, you've found out, use

@propertysource("/web-inf/config/prod.properties") // or support property @propertysource("${prodpropertiesfile}") 

which attempt retrieve resource through servletcontext. javadoc states

the path must begin / , interpreted relative current context root

your web-inf folder in context root (basically directory chosen servlet container root of web application) , resource found , used.


you can read more resource in official documentation, here, , resourceloader process, here.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -