org.springframework.beans.factory.access.BeanFactoryReference Java Examples

The following examples show how to use org.springframework.beans.factory.access.BeanFactoryReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextJndiBeanFactoryLocator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #2
Source File: ContextJndiBeanFactoryLocator.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #3
Source File: ContextSingletonBeanFactoryLocatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: SpringBeanAutowiringInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example #5
Source File: SpringBeanAutowiringInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example #6
Source File: ComponentLoaderImpl.java    From zstack with Apache License 2.0 5 votes vote down vote up
public ComponentLoaderImpl () {
    checkInit();
    BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator
            .getInstance(String.format("classpath:%s", CoreGlobalProperty.BEAN_REF_CONTEXT_CONF));
    BeanFactoryReference ref = factoryLocator.useBeanFactory("parentContext");
    ioc = ref.getFactory();
}
 
Example #7
Source File: EtlExecutorBean.java    From scriptella-etl with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: SpringBeanAutowiringInterceptor.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: SpringBeanAutowiringInterceptor.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example #10
Source File: ContextJndiBeanFactoryLocator.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #11
Source File: ContextJndiBeanFactoryLocator.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}