Reputation: 71
I have written a factory bean that creates a cache manager based on the properties that are configured in a application specific properties file.
The concept is that multiple implementations can be chosen, each using other configuration properties.
For example:
I think it is nice to not specify all cache-application specific parameters in the application-context.xml
, but read them from the existing properties sources.
My attempt was using a EnvironementAware
interface to get access to the Environement
. But it seems that the property file that is configured using <context:property-placeholder>
is not contained in the 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"/>
In 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;
}
}
Upvotes: 3
Views: 5951
Reputation: 47280
In config file like this :
<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>
or in code :
@Value("${cities}")
private String cities;
where the your.properties contains this :
cities = my test string
property1.email = [email protected]
Upvotes: 3