gpt4 book ai didi

java - 访问ehcache.xml中的spring环境变量

转载 作者:行者123 更新时间:2023-12-01 09:55:04 44 4
gpt4 key购买 nike

我想访问 ehcache.xml 中的 Spring 环境变量,但出现以下错误。

    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)

我还运行了以下测试,该测试显示系统属性可用于 ehcache.xml。任何想法?谢谢。

 @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);

}

最佳答案

Ehcache 属性占位符是从系统属性解析的,而 Spring 属性抽象使用多个源,即系统属性、属性文件和系统环境。

第一个选项是在运行应用程序时使用系统属性,即 -Dcache.report.maxMemoryElements=4446

如果这不是一个选项,并且您确实需要使用 Spring 属性文件,您可以实现一个 BeanFactoryPostProcessor,它将您的 Spring 属性文件复制到系统属性中。例如

    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);
}
}
}

}

然后在你的配置文件中

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

关于java - 访问ehcache.xml中的spring环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37301017/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com