我编写了一个工厂bean,它根据应用程序特定的属性文件中配置的属性创建一个缓存管理器.
概念是可以选择多个实现,每个实现都使用其他配置属性.
例如:
> noop缓存,不带参数,
> ehcache与#max个对象
>具有多个ip和端口配置的内存缓存.
我认为最好不要在application-context.xml中指定所有特定于缓存的应用程序参数,而是从现有属性源中读取它们.
我的尝试是使用EnvironementAware接口来访问环境.但是似乎使用< context:property-placeholder>配置的属性文件不包含在PropertiesSources中.
example.properties
cache.implementation=memcached
cache.memcached.servers=server1:11211,server2:11211
application-context.xml
<context:property-placeholder location="example.properties"/>
<bean id="cacheManager" class="com.example.CacheManagerFactory"/>
在CacheManagerFactory.java中
public class CacheManagerFactory implements factorybean<CacheManager>,EnvironmentAware {
private Environement env;
@Override
public CacheManager getobject() throws Exception {
String impl = env.getrequiredProperty("cache.implementation"); // this fails
//Do something based on impl,which requires more properties.
}
@Override
public void setEnvironment(Environment env) {
this.env = env;
}
@Override
public Class<?> getobjectType() {
return CacheManager.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
最佳答案
在这样的配置文件中:
<context:property-placeholder location="classpath:your.properties" ignore-unresolvable="true"/>
...
<property name="email" value="${property1.email}"/>
<!-- or -->
<property name="email">
<value>${property1.email}</value>
</property>
或在代码中:
@Value("${cities}")
private String cities;
your.properties包含以下内容:
cities = my test string
property1.email = [email protected]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。