Dreamer
Dreamer

Reputation: 7551

class path resource [classpath*:xxxxx.properties] cannot be opened because it does not exist

I am trying to implement sort of conditional import bean context files within current spring application. To make this work, I use similar solution in SO to extend the ApplicationContextInitializer<ConfigurableApplicationContext>

package com.mydepartment.app.util;

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

   @Override
   public void initialize(ConfigurableApplicationContext applicationContext) {
      ConfigurableEnvironment env = applicationContext.getEnvironment();

      // load from web.xml context-param
      String propertyFileClassPath = env.getProperty("propertyFileClassPath");

      try {
          MutablePropertySources sources = env.getPropertySources();
          sources.addFirst(new ResourcePropertySource(new ClassPathResource(propertyFileClassPath)));
      } catch (IOException ioException) {
         LOG.info(ioException.getMessage());
         LOG.error( "Loading resource failed..", ioException);
      }
   }
}

And this is want I add into web.xml to make current application to be able to load the properties file which contains the properties which defines the condition

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.mydepartment.app.util.MyApplicationContextInitializer</param-value>
</context-param>
<context-param>
    <param-name>propertyFileClassPath</param-name>
    <param-value>classpath*:application-env.properties</param-value>
</context-param>

.....
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

the application-env.properties resides in

%war_file_name%\WEB-INF\classes\application-env.properties

but above configuration/coding returns following error in both WebLogic and Websphere, which makes me really miserable for the fix:

2015-10-15 13:23:12,625 -- INFO -- com.mydepartment.app.util.MyApplicationContextInitializer -- class path resource [classpath*:application-env.properties] cannot be opened because it does not exist
2015-10-15 13:23:12,626 -- ERROR -- com.mydepartment.app.util.MyApplicationContextInitializer -- Loading resource failed..

java.io.FileNotFoundException: class path resource [classpath*:application-env.properties] cannot be opened because it does not exist

I tried several pattern to establish the classpath but all failed, such as:

classpath*:application-env.properties
file:/WEB-INF/classes/application-env.properties
/WEB-INF/classes/application-env.properties
../classes/application-env.properties

Potentially I want the application to load the init properties from properties file inside the application, instead of system properties or server properties. But I don't see the necessity to add the properties file into the server's classpath.

I don't see any reason that the application server blocks access to this internal properties file.

Upvotes: 1

Views: 6313

Answers (1)

Mohit
Mohit

Reputation: 1755

When we use ClassPathResource, we directly specify the resource path. Try the following:

ClassPathResource cr = new ClassPathResource("application-env.properties");

Upvotes: 1

Related Questions