jd466
jd466

Reputation: 579

Accessing spring environmental variables in ehcache.xml

I would like to access Spring environmental variables in ehcache.xml, but I get the following error.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [com/cache.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Error configuring from input stream. Initial cause was null:166: Could not set attribute "maxElementsInMemory" - For input string: "${cache.report.maxMemoryElements}"
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
        at 

org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:672)
        at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1861)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

I also run the bellow test, which shows that the System Properties are available for ehcache.xml. Any Idea? Thanks.

 @Test
  public void testLoadConfigurationWithReplacement() throws Exception {
    System.setProperty("cache.report.maxMemoryElements", "4446");
    CacheManager manager = CacheManager.newInstance(TEST_CONFIG_DIR + "ehcache.xml");
    System.out.print("caches: " + manager.getCacheNames());
    assertEquals(manager.getCache("reportCache").getCacheConfiguration().getMaxEntriesLocalHeap(), 4446);

  }

Upvotes: 2

Views: 2024

Answers (1)

ekem chitsiga
ekem chitsiga

Reputation: 5753

Ehcache property placeholders are resolved from System properties whilst Spring property abstraction uses several sources i.e system properties , properties file and system environment.

The first option you have is to use a system property when you run your application i.e -Dcache.report.maxMemoryElements=4446

If this is not an option and you really need to use Spring properties file you can implement a BeanFactoryPostProcessor which copies your Spring prperties file into system properties. For example

    public class  EhachePropertiesReplacer implements BeanFactoryPostProcessor , EnvironmentAware{

       private Environment environment;

       private List<String> ehcachePropertyNames;

       public void setEhcahePropertyNames(List<String> ehcachePropertyNames){
          this.ehcachePropertyNames = ehcachePropertyNames;
       }

       public void setEnvironment(Environment environment){
          this.environment = environment;
       }

       public void postProcessBeanFactory(ConfigurableBeanFactory bf){
          for(String ehcahePropertyName : ehcahePropetyNames){
             String ehcachePropertyValue = evironment.getProperty(ehcahePropertyName);
             System.setProperty(ehcahePropertyName ,ehcachePropertyValue);
          }      
       }
   }

}

Then in your config file

<bean class= "whatever.the.package.EhachePropertiesReplacer">
  <property name="ehachePropertyNames">
     <list>
        <value>cache.report.maxMemoryElements</value>
     </list>
  </property>
</bean>

Upvotes: 2

Related Questions