下面列出了怎么用org.springframework.beans.factory.access.BeanFactoryReference的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Load/use a bean factory, as specified by a factory key which is a JNDI
* address, of the form {@code java:comp/env/ejb/BeanFactoryPath}. The
* contents of this JNDI location must be a string containing one or more
* classpath resource names (separated by any of the delimiters '{@code ,; \t\n}'
* if there is more than one. The resulting BeanFactory (or ApplicationContext)
* will be created from the combined resources.
* @see #createBeanFactory
*/
@Override
public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException {
try {
String beanFactoryPath = lookup(factoryKey, String.class);
if (logger.isTraceEnabled()) {
logger.trace("Bean factory path from JNDI environment variable [" + factoryKey +
"] is: " + beanFactoryPath);
}
String[] paths = StringUtils.tokenizeToStringArray(beanFactoryPath, BEAN_FACTORY_PATH_DELIMITERS);
return createBeanFactory(paths);
}
catch (NamingException ex) {
throw new BootstrapException("Define an environment variable [" + factoryKey + "] containing " +
"the class path locations of XML bean definition files", ex);
}
}
/**
* Load/use a bean factory, as specified by a factory key which is a JNDI
* address, of the form {@code java:comp/env/ejb/BeanFactoryPath}. The
* contents of this JNDI location must be a string containing one or more
* classpath resource names (separated by any of the delimiters '{@code ,; \t\n}'
* if there is more than one. The resulting BeanFactory (or ApplicationContext)
* will be created from the combined resources.
* @see #createBeanFactory
*/
@Override
public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException {
try {
String beanFactoryPath = lookup(factoryKey, String.class);
if (logger.isTraceEnabled()) {
logger.trace("Bean factory path from JNDI environment variable [" + factoryKey +
"] is: " + beanFactoryPath);
}
String[] paths = StringUtils.tokenizeToStringArray(beanFactoryPath, BEAN_FACTORY_PATH_DELIMITERS);
return createBeanFactory(paths);
}
catch (NamingException ex) {
throw new BootstrapException("Define an environment variable [" + factoryKey + "] containing " +
"the class path locations of XML bean definition files", ex);
}
}
@Override
@Test
public void testBasicFunctionality() {
ContextSingletonBeanFactoryLocator facLoc = new ContextSingletonBeanFactoryLocator(
"classpath*:" + ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT));
basicFunctionalityTest(facLoc);
BeanFactoryReference bfr = facLoc.useBeanFactory("a.qualified.name.of.some.sort");
BeanFactory fac = bfr.getFactory();
assertTrue(fac instanceof ApplicationContext);
assertEquals("a.qualified.name.of.some.sort", ((ApplicationContext) fac).getId());
assertTrue(((ApplicationContext) fac).getDisplayName().contains("a.qualified.name.of.some.sort"));
BeanFactoryReference bfr2 = facLoc.useBeanFactory("another.qualified.name");
BeanFactory fac2 = bfr2.getFactory();
assertEquals("another.qualified.name", ((ApplicationContext) fac2).getId());
assertTrue(((ApplicationContext) fac2).getDisplayName().contains("another.qualified.name"));
assertTrue(fac2 instanceof ApplicationContext);
}
/**
* Actually release the BeanFactoryReference for the given target bean.
* @param target the target bean to release
*/
protected void doReleaseBean(Object target) {
BeanFactoryReference ref = this.beanFactoryReferences.remove(target);
if (ref != null) {
ref.release();
}
}
/**
* Actually release the BeanFactoryReference for the given target bean.
* @param target the target bean to release
*/
protected void doReleaseBean(Object target) {
BeanFactoryReference ref = this.beanFactoryReferences.remove(target);
if (ref != null) {
ref.release();
}
}
public ComponentLoaderImpl () {
checkInit();
BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator
.getInstance(String.format("classpath:%s", CoreGlobalProperty.BEAN_REF_CONTEXT_CONF));
BeanFactoryReference ref = factoryLocator.useBeanFactory("parentContext");
ioc = ref.getFactory();
}
/**
* This method obtains a global ThreadLocal class independent of the classloader (JVM-scope singleton).
* The easiest solution is to use System.getProperties().get/put, but this solution violate
* Properties contract and have other drawbacks.
* <p>Current solution relies on the idea behind
* {@link org.springframework.beans.factory.access.SingletonBeanFactoryLocator}. See also bug #4648
*
* @return Global ThreadLocal (JVM-scope singleton).
*/
@SuppressWarnings("unchecked")
private static ThreadLocal<BeanFactory> getGlobalThreadLocal() {
BeanFactoryLocator locator = SingletonBeanFactoryLocator.getInstance(BEAN_FACTORY_XML_PATH);
BeanFactoryReference ref = locator.useBeanFactory(FACTORY_BEAN_NAME);
StaticApplicationContext ctx = (StaticApplicationContext) ref.getFactory();
if (!ctx.containsBean(THREAD_LOCAL_BEAN_NAME)) {
ctx.registerSingleton(THREAD_LOCAL_BEAN_NAME, ThreadLocal.class);
}
return (ThreadLocal) ctx.getBean(THREAD_LOCAL_BEAN_NAME);
}
/**
* Determine the BeanFactoryReference for the given target bean.
* <p>The default implementation delegates to {@link #getBeanFactoryLocator}
* and {@link #getBeanFactoryLocatorKey}.
* @param target the target bean to autowire
* @return the BeanFactoryReference to use (never {@code null})
* @see #getBeanFactoryLocator
* @see #getBeanFactoryLocatorKey
* @see org.springframework.beans.factory.access.BeanFactoryLocator#useBeanFactory(String)
*/
protected BeanFactoryReference getBeanFactoryReference(Object target) {
String key = getBeanFactoryLocatorKey(target);
BeanFactoryReference ref = getBeanFactoryLocator(target).useBeanFactory(key);
this.beanFactoryReferences.put(target, ref);
return ref;
}
/**
* Determine the BeanFactoryReference for the given target bean.
* <p>The default implementation delegates to {@link #getBeanFactoryLocator}
* and {@link #getBeanFactoryLocatorKey}.
* @param target the target bean to autowire
* @return the BeanFactoryReference to use (never {@code null})
* @see #getBeanFactoryLocator
* @see #getBeanFactoryLocatorKey
* @see org.springframework.beans.factory.access.BeanFactoryLocator#useBeanFactory(String)
*/
protected BeanFactoryReference getBeanFactoryReference(Object target) {
String key = getBeanFactoryLocatorKey(target);
BeanFactoryReference ref = getBeanFactoryLocator(target).useBeanFactory(key);
this.beanFactoryReferences.put(target, ref);
return ref;
}
/**
* Create the BeanFactory instance, given an array of class path resource Strings
* which should be combined. This is split out as a separate method so that
* subclasses can override the actual BeanFactory implementation class.
* <p>Delegates to {@code createApplicationContext} by default,
* wrapping the result in a ContextBeanFactoryReference.
* @param resources an array of Strings representing classpath resource names
* @return the created BeanFactory, wrapped in a BeanFactoryReference
* (for example, a ContextBeanFactoryReference wrapping an ApplicationContext)
* @throws BeansException if factory creation failed
* @see #createApplicationContext
* @see ContextBeanFactoryReference
*/
protected BeanFactoryReference createBeanFactory(String[] resources) throws BeansException {
ApplicationContext ctx = createApplicationContext(resources);
return new ContextBeanFactoryReference(ctx);
}
/**
* Create the BeanFactory instance, given an array of class path resource Strings
* which should be combined. This is split out as a separate method so that
* subclasses can override the actual BeanFactory implementation class.
* <p>Delegates to {@code createApplicationContext} by default,
* wrapping the result in a ContextBeanFactoryReference.
* @param resources an array of Strings representing classpath resource names
* @return the created BeanFactory, wrapped in a BeanFactoryReference
* (for example, a ContextBeanFactoryReference wrapping an ApplicationContext)
* @throws BeansException if factory creation failed
* @see #createApplicationContext
* @see ContextBeanFactoryReference
*/
protected BeanFactoryReference createBeanFactory(String[] resources) throws BeansException {
ApplicationContext ctx = createApplicationContext(resources);
return new ContextBeanFactoryReference(ctx);
}