1 16 17 package org.springframework.context.access; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.springframework.beans.BeansException; 23 import org.springframework.beans.factory.BeanFactory; 24 import org.springframework.beans.factory.access.BeanFactoryLocator; 25 import org.springframework.beans.factory.access.SingletonBeanFactoryLocator; 26 import org.springframework.context.ConfigurableApplicationContext; 27 import org.springframework.context.support.ClassPathXmlApplicationContext; 28 import org.springframework.core.io.support.ResourcePatternResolver; 29 import org.springframework.core.io.support.ResourcePatternUtils; 30 31 49 public class ContextSingletonBeanFactoryLocator extends SingletonBeanFactoryLocator { 50 51 private static final String BEANS_REFS_XML_NAME = "classpath*:beanRefContext.xml"; 52 53 private static final Map instances = new HashMap (); 55 56 57 63 public static BeanFactoryLocator getInstance() throws BeansException { 64 return getInstance(BEANS_REFS_XML_NAME); 65 } 66 67 81 public static BeanFactoryLocator getInstance(String selector) throws BeansException { 82 if (!ResourcePatternUtils.isUrl(selector)) { 85 selector = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + selector; 86 } 87 88 synchronized (instances) { 89 if (logger.isTraceEnabled()) { 90 logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + 91 instances.hashCode() + ", instances=" + instances); 92 } 93 BeanFactoryLocator bfl = (BeanFactoryLocator) instances.get(selector); 94 if (bfl == null) { 95 bfl = new ContextSingletonBeanFactoryLocator(selector); 96 instances.put(selector, bfl); 97 } 98 return bfl; 99 } 100 } 101 102 103 108 protected ContextSingletonBeanFactoryLocator() { 109 super(BEANS_REFS_XML_NAME); 110 } 111 112 117 protected ContextSingletonBeanFactoryLocator(String resourceName) { 118 super(resourceName); 119 } 120 121 126 protected BeanFactory createDefinition(String resourceName, String factoryKey) throws BeansException { 127 return new ClassPathXmlApplicationContext(new String [] {resourceName}, false); 128 } 129 130 135 protected void initializeDefinition(BeanFactory groupDef) throws BeansException { 136 if (groupDef instanceof ConfigurableApplicationContext) { 137 ((ConfigurableApplicationContext) groupDef).refresh(); 138 } 139 } 140 141 144 protected void destroyDefinition(BeanFactory groupDef, String resourceName) throws BeansException { 145 if (groupDef instanceof ConfigurableApplicationContext) { 146 if (logger.isTraceEnabled()) { 147 logger.trace("ContextSingletonBeanFactoryLocator group with resourceName '" + 148 resourceName + "' being released, as there are no more references"); 149 } 150 ((ConfigurableApplicationContext) groupDef).close(); 151 } 152 } 153 154 } 155 | Popular Tags |