All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.context.access.ContextSingletonBeanFactoryLocator Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.access;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;

/**
 * 

Variant of SingletonBeanFactoryLocator which creates its internal bean * factory reference definition as an ApplicationContext instead of * SingletonBeanFactoryLocator's BeanFactory. For almost all usage scenarios, this * will not make a difference, since within that ApplicationContext or BeanFactory * you are still free to create either BeanFactories or ApplicationContexts. The * main reason one would need to use this class is if bean post-processing (or other * ApplicationContext specific features are needed in the bean reference definition * itself). * *

Note: This class uses classpath*:beanRefContext.xml * as the default name for the bean factory reference definition. It is not possible * nor legal to share definitions with SingletonBeanFactoryLocator at the same time. * * @author Colin Sampaleanu * @author Juergen Hoeller * @see org.springframework.context.access.DefaultLocatorFactory */ public class ContextSingletonBeanFactoryLocator extends SingletonBeanFactoryLocator { private static final String BEANS_REFS_XML_NAME = "classpath*:beanRefContext.xml"; // the keyed singleton instances private static final Map instances = new HashMap(); /** * Returns an instance which uses the default "classpath*:beanRefContext.xml", as * the name of the definition file(s). All resources returned by the current * thread's context class loader's getResources method with this * name will be combined to create a definition, which is just a BeanFactory. */ public static BeanFactoryLocator getInstance() throws BeansException { return getInstance(BEANS_REFS_XML_NAME); } /** * Returns an instance which uses the the specified selector, as the name of the * definition file(s). In the case of a name with a Spring "classpath*:" prefix, * or with no prefix, which is treated the same, the current thread's context class * loader's getResources method will be called with this value to get * all resources having that name. These resources will then be combined to form a * definition. In the case where the name uses a Spring "classpath:" prefix, or * a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the name of the resource(s) which will be read and combine to * form the definition for the SingletonBeanFactoryLocator instance. The one file * or multiple fragments with this name must form a valid ApplicationContext * definition. */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { // For backwards compatibility, we prepend "classpath*:" to the selector name if there // is no other prefix (i.e. "classpath*:", "classpath:", or some URL prefix). if (!ResourcePatternUtils.isUrl(selector)) { selector = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + selector; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = (BeanFactoryLocator) instances.get(selector); if (bfl == null) { bfl = new ContextSingletonBeanFactoryLocator(selector); instances.put(selector, bfl); } return bfl; } } /** * Constructor which uses the default "classpath*:beanRefContext.xml", as the name of the * definition file(s). All resources returned by the definition classloader's * getResources() method with this name will be combined to create a definition. */ protected ContextSingletonBeanFactoryLocator() { super(BEANS_REFS_XML_NAME); } /** * Constructor which uses the the specified name as the name of the definition file(s). * All resources returned by the definition ClassLoader's getResources * method with this name will be combined to create a definition. */ protected ContextSingletonBeanFactoryLocator(String resourceName) { super(resourceName); } /** * Overrides default method to create definition object as an ApplicationContext * instead of the default BeanFactory. This does not affect what can actually * be loaded by that definition. */ protected BeanFactory createDefinition(String resourceName, String factoryKey) throws BeansException { return new ClassPathXmlApplicationContext(new String[] {resourceName}, false); } /** * Overrides default method to initialize definition object, which this method * assumes is a ConfigurableApplicationContext. This implementation simply invokes * {@link ConfigurableApplicationContext#refresh ConfigurableApplicationContext.refresh()}. */ protected void initializeDefinition(BeanFactory groupDef) throws BeansException { if (groupDef instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) groupDef).refresh(); } } /** * Overrides default method to work with an ApplicationContext. */ protected void destroyDefinition(BeanFactory groupDef, String resourceName) throws BeansException { if (groupDef instanceof ConfigurableApplicationContext) { if (logger.isTraceEnabled()) { logger.trace("ContextSingletonBeanFactoryLocator group with resourceName '" + resourceName + "' being released, as there are no more references"); } ((ConfigurableApplicationContext) groupDef).close(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy