The PropertiesAccessor below provides access to properties loaded by Property Placeholder and encapsulates container specific stuff. It also caches found properties because call on AbstractBeanFactory#resolveEmbeddedValue(String) is not cheap.
@Named public class PropertiesAccessor { private final AbstractBeanFactory beanFactory; private final Map<String,String> cache = new ConcurrentHashMap<>(); @Inject protected PropertiesAccessor(AbstractBeanFactory beanFactory) { this.beanFactory = beanFactory; } public String getProperty(String key) { if(cache.containsKey(key)){ return cache.get(key); } String foundProp = null; try { foundProp = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}"); cache.put(key,foundProp); } catch (IllegalArgumentException ex) { // ok - property was not found } return foundProp; } }
No comments:
Post a Comment