java Spring上下文属性占位符ehcahe配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1533409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring context property-placholder ehcahe configuration
提问by Dennis S
I have a spring context xml file with this
我有一个带有这个的 spring 上下文 xml 文件
<context:property-placeholder location="classpath:cacheConfig.properties"/>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="cacheManagerName" value="cacheName"/>
<property name="shared" value="false"/>
<property name="configLocation" value="classpath:cacheConfig.xml"/>
</bean>
the goal is to allow the customer to edit the properties file, like this
目标是允许客户像这样编辑属性文件
cache.maxMemoryElements="2000"
and then in the actual cacheConfig.xml file have this
然后在实际的 cacheConfig.xml 文件中有这个
<cache name="someCacheName"
maxElementsInMemory="${cache.maxMemoryElements}" ... />
so that items we do not want the customer to change are not exposed. Of course the above details are only partially detailed and NOT working. Currently I see this in the log file
以便我们不希望客户更改的项目不会暴露。当然,上面的细节只是部分详细而不是有效的。目前我在日志文件中看到了这一点
Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Error configuring from input stream. Initial cause was null:149: Could not set attribute "maxElementsInMemory".
Thanks in advance...
提前致谢...
回答by skaffman
Your example uses EhCacheManagerFactoryBeanto expose a reference to the CacheManager, with caches defined in the external cacheConfig.xmlfile. As @ChssPly76 pointed out, Spring's property resolver only works within Spring's own bean definition files.
您的示例用于EhCacheManagerFactoryBean公开对CacheManager,的引用,并在外部cacheConfig.xml文件中定义缓存。正如@ChssPly76 指出的那样,Spring 的属性解析器仅适用于 Spring 自己的 bean 定义文件。
However, you don't have to define the individual caches in the external file, you can define them right within the Spring bean definition file, using EhCacheFactoryBean:
但是,您不必在外部文件中定义各个缓存,您可以直接在 Spring bean 定义文件中定义它们,使用EhCacheFactoryBean:
FactoryBean that creates a named EHCache Cache instance... If the specified named cache is not configured in the cache configuration descriptor, this FactoryBean will construct an instance of a Cache with the provided name and the specified cache properties and add it to the CacheManager for later retrieval.
创建命名 EHCache Cache 实例的 FactoryBean... 如果在缓存配置描述符中未配置指定的命名缓存,则此 FactoryBean 将使用提供的名称和指定的缓存属性构造一个 Cache 实例,并将其添加到 CacheManager 中后来检索。
In other words, if you use EhCacheFactoryBeanto refer to a named cache that isn't already defined in cacheConfig.xml, then Spring will create and configure a new cache instance and register it with the CacheManagerat runtime. That includes specifying things like maxElementsInMemory, and because this would be specified in the Spring bean definition file, you get full support of the property resolver:
换句话说,如果您使用EhCacheFactoryBean引用尚未在 中定义的命名缓存cacheConfig.xml,那么 Spring 将创建并配置一个新的缓存实例,并CacheManager在运行时将其注册到 。这包括指定诸如 之类的内容maxElementsInMemory,并且因为这将在 Spring bean 定义文件中指定,您将获得对属性解析器的完全支持:
<context:property-placeholder location="classpath:cacheConfig.properties"/>
<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager"/>
<property name="maxElementsInMemory" value="${cache.maxMemoryElements}"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="shared" value="false"/>
<property name="configLocation" value="classpath:cacheConfig.xml"/>
</bean>
回答by ChssPly76
This is not how PropertyPlaceholderConfigurer works. It can be used to replace values within context, but not within arbitrary external files. And cacheConfig.xmlis an external file - it's just being passed by Spring to EH Cache.
这不是 PropertyPlaceholderConfigurer 的工作方式。它可用于替换context 中的值,但不能用于替换任意外部文件中的值。并且cacheConfig.xml是一个外部文件 - 它只是由 Spring 传递给 EH 缓存。
回答by matt b
If you are using Maven or Ant, both offer the ability to do filtering of tokens in resource files.
如果您使用 Maven 或 Ant,两者都提供对资源文件中的令牌进行过滤的能力。
For Maven, you could do something like
对于 Maven,你可以做类似的事情
<cache name="someCacheName"
maxElementsInMemory="${cache.maxMemoryElements}" ... />
And in a filter file, or in the POM itself, have
并且在过滤器文件中,或者在 POM 本身中,有
cache.maxMemoryElements = 200
Resource Filtering in Maven: The Definitive Guide
With Ant, you do this with FilterSetsand the <copy>task.
使用 Ant,您可以使用FilterSets和<copy>任务来执行此操作。
回答by Seb
For anyone who needs to modify the diskstore path which cannot be set as the ehcache javadoc states that the diskstore parameter is ignored, you could create your own implementation of a EhCacheManagerFactoryBean, which allows you to inject the diskstore path; you basically need to intercept the creation of the CacheManager and modify the configuration passed along using your diskstore property, e.g.:
对于任何需要修改无法设置为 ehcache javadoc 的磁盘存储路径的人来说,磁盘存储参数被忽略,您可以创建自己的 EhCacheManagerFactoryBean 实现,它允许您注入磁盘存储路径;您基本上需要拦截 CacheManager 的创建并修改使用您的磁盘存储属性传递的配置,例如:
private String diskStorePath;
...getter/setter
public void afterPropertiesSet() throws IOException, CacheException {
if (this.shared) {
// Shared CacheManager singleton at the VM level.
if (this.configLocation != null) {
this.cacheManager = CacheManager.create(this.createConfig());
}
else {
this.cacheManager = CacheManager.create();
}
}
else {
// Independent CacheManager instance (the default).
if (this.configLocation != null) {
this.cacheManager = new CacheManager(this.createConfig());
}
else {
this.cacheManager = new CacheManager();
}
}
if (this.cacheManagerName != null) {
this.cacheManager.setName(this.cacheManagerName);
}
}
private Configuration createConfig() throws CacheException, IOException {
Configuration config = ConfigurationFactory.parseConfiguration(this.configLocation.getInputStream());
DiskStoreConfiguration diskStoreConfiguration = config.getDiskStoreConfiguration();
if (diskStoreConfiguration == null) {
DiskStoreConfiguration diskStoreConfigurationParameter = new DiskStoreConfiguration();
diskStoreConfigurationParameter.setPath(getDiskStorePath());
config.addDiskStore(diskStoreConfigurationParameter);
} else {
diskStoreConfiguration.setPath(getDiskStorePath());
}
return config;
}
The Spring config then looks like this:
Spring 配置如下所示:
<bean id="cacheManager" class="com.yourcompany.package.MyEhCacheManagerFactoryBean" depends-on="placeholderConfig">
<property name="diskStorePath" value="${diskstore.path}"/>
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>

