Announcement Announcement Module
Collapse
No announcement yet.
PropertySourcesPlaceholderConfigurer: resolving placeholders for locations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertySourcesPlaceholderConfigurer: resolving placeholders for locations

    Greetings,
    I am looking to resolve placeholders when setting location(s) on PropertySourcesPlaceholderConfigurer. Here's my bean definition:

    Code:
    <bean id="dbConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" lazy-init="false">
        <property name="location" value="file:${pm.home}/db.config" />
    </bean>
    The idea is that it will resolve ${pm.home} from the environment (which works), or from classpath: pm.properties. After tracing thru the code, I found that XmlWebApplicationContext instantiates StandardServletEnvironment which is ultimately used when resolving the environment variable in ResourceEditor.resolvePath(String path). So, it seems that if I can just get StandardServletEnvironment to load pm.properties, then my problem will be solved. Am I headed in the right direction, or is there a better/simpler way of doing this? Thanks.

  • #2
    Create an implementation of an ApplicationContextInitializer and add a ResourcePropertySource which contains the content loaded from the pm.properties.
    Last edited by Marten Deinum; Jun 11th, 2013, 04:06 AM. Reason: Changed to ResourcePropertySource and added links.

    Comment


    • #3
      Solution

      Very good, sir. Works exactly as described. For the sake of clarity, here's the actual solution...

      Code:
      package pm;
      
      import java.io.IOException;
      
      import org.springframework.context.ApplicationContextInitializer;
      import org.springframework.context.ConfigurableApplicationContext;
      import org.springframework.core.io.support.ResourcePropertySource;
      
      public class ContextInitializer
      implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext applicationContext) {    
          try {
            applicationContext
              .getEnvironment()
              .getPropertySources()
              .addLast(new ResourcePropertySource("classpath:pm.properties"));
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
      web.xml
      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app>
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          <context-param>
              <param-name>contextInitializerClasses</param-name>
              <param-value>pm.ContextInitializer</param-value>
          </context-param>
      </web-app>
      Last edited by bsanders1979; Jun 11th, 2013, 11:07 PM. Reason: Unnecessary annotation

      Comment

      Working...
      X