+ add flag to disable bean-factory-locator
This commit is contained in:
Costin Leau
2011-07-06 11:50:55 +03:00
parent fff04a8777
commit e261ecf6bd
7 changed files with 59 additions and 13 deletions

View File

@@ -62,17 +62,21 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl
private Properties properties;
private DistributedSystem system;
private ClassLoader beanClassLoader;
private GemfireBeanFactoryLocator factoryLocator = new GemfireBeanFactoryLocator();
private GemfireBeanFactoryLocator factoryLocator;
private BeanFactory beanFactory;
private String beanName;
private boolean useBeanFactoryLocator = true;
public void afterPropertiesSet() throws Exception {
// initialize locator
factoryLocator.setBeanFactory(beanFactory);
factoryLocator.setBeanName(beanName);
factoryLocator.afterPropertiesSet();
if (useBeanFactoryLocator) {
factoryLocator = new GemfireBeanFactoryLocator();
factoryLocator.setBeanFactory(beanFactory);
factoryLocator.setBeanName(beanName);
factoryLocator.afterPropertiesSet();
}
Properties cfgProps = mergeProperties();
system = DistributedSystem.connect(cfgProps);
@@ -128,7 +132,10 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl
}
system = null;
factoryLocator.destroy();
if (factoryLocator != null) {
factoryLocator.destroy();
factoryLocator = null;
}
}
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
@@ -187,4 +194,15 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl
public void setCacheXml(Resource cacheXml) {
this.cacheXml = cacheXml;
}
/**
* Indicates whether a bean factory locator is enabled (default) for this cache definition or not. The locator stores
* the enclosing bean factory reference to allow auto-wiring of Spring beans into GemFire managed classes. Usually disabled
* when the same cache is used in multiple application context/bean factories inside the same VM.
*
* @param usage true if the bean factory locator is used underneath or not
*/
public void setUseBeanFactoryLocator(boolean usage) {
this.useBeanFactoryLocator = usage;
}
}

View File

@@ -112,7 +112,8 @@ public class GemfireBeanFactoryLocator implements BeanFactoryLocator, BeanFactor
if (log.isDebugEnabled())
log.debug("adding key=" + name + " w/ reference=" + beanFactory);
if (beanFactories.containsKey(name) || beanFactories.putIfAbsent(name, beanFactory) != null) {
if (beanFactories.containsKey(name) && !beanFactory.equals(beanFactories.get(name))
|| beanFactories.putIfAbsent(name, beanFactory) != null) {
throw new IllegalArgumentException("a beanFactoryReference already exists for key " + factoryName);
}
}
@@ -120,10 +121,11 @@ public class GemfireBeanFactoryLocator implements BeanFactoryLocator, BeanFactor
}
public void destroy() {
for (String name : names) {
beanFactories.remove(name);
if (names != null) {
for (String name : names) {
beanFactories.remove(name);
}
}
if (beanFactory == defaultFactory) {
synchronized (GemfireBeanFactoryLocator.class) {
defaultFactory = null;