Announcement Announcement Module
No announcement yet.
Override property holders at runtime at application context creation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Override property holders at runtime at application context creation

    I have the simple requirement that I wish to be able to connect to an RMI service for which the host name is only known at runtime. I have the feeling though that the solution (as with some more "dynamic" things in Spring) is not easy.
    (The question was also asked here, but I don't really see a good solution there.)

    Anyway, a part of the applicationContext.xml is:
    	<bean id="remotePropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    	<bean id="itemDaoClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    		<property name="serviceUrl" value="rmi://${remote.server}/ItemDao" />
    		<property name="serviceInterface" value="IItemDao" />
    Fortunately the host name is known before the context is created:

    String hostName = args[0];
    // TODO: put this hostName in the place of ${remote.server}
    ApplicationContext context = new ClassPathXmlApplicationContext(new  String[] {"/META-INF/spring/applicationContext.xml"});
    IItemDao itemDao = (IItemDao) context.getBean("itemDaoClient");
    Is there an elegant way to override the ${remote.server} value found in the with that in the hostName variable? Preferably without having to change the xml file? If not, what would be the next best thing to do without resorting to moving the RMI lookup to Java code?

  • #2
    Found a way

    Okay, I finally found one way to do it myself. We can replace the PropertyPlaceholderConfigurer by a PreferencesPlaceholderConfigurer which looks in the user preferences first. It works if I add this line before creating the Spring context:

    Preferences.userRoot().put("remote.server", serverHost);
    Not sure if this is the best way to do it though.

    Another way would be to configure the PropertyPlaceholderConfigurer to look in the system properties BEFORE looking in the properties file by setting property systemPropertiesMode to SYSTEM_PROPERTIES_MODE_OVERRIDE which I haven't tested yet.


    • #3
      Besides the solutions you discovered yourself, you can also create your own BeanFactoryPostProcessor which, based on the PPC code, can use different fallback strategies.
      It requires you to write some code but overall, gives you maximum flexibility if the out-of-the-box solutions are not good enough.