Java Code Examples for org.springframework.beans.factory.config.ConfigurableBeanFactory

The following examples show how to use org.springframework.beans.factory.config.ConfigurableBeanFactory. These examples are extracted from open source projects. 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
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
	ConfigurableBeanFactory beanFactory = (this.beanFactory instanceof ConfigurableBeanFactory ?
			(ConfigurableBeanFactory) this.beanFactory : null);

	// Annotation-based argument resolution
	resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
	resolvers.add(new HeadersMethodArgumentResolver());

	// Type-based argument resolution
	resolvers.add(new MessageMethodArgumentResolver(this.messageConverter));

	if (this.customArgumentResolvers != null) {
		resolvers.addAll(this.customArgumentResolvers);
	}

	Assert.notNull(this.messageConverter, "MessageConverter not configured");
	resolvers.add(new PayloadMethodArgumentResolver(this.messageConverter, this.validator));

	return resolvers;
}
 
Example 2
@Override
public Object getAspectCreationMutex() {
	if (this.beanFactory != null) {
		if (this.beanFactory.isSingleton(name)) {
			// Rely on singleton semantics provided by the factory -> no local lock.
			return null;
		}
		else if (this.beanFactory instanceof ConfigurableBeanFactory) {
			// No singleton guarantees from the factory -> let's lock locally but
			// reuse the factory's singleton lock, just in case a lazy dependency
			// of our advice bean happens to trigger the singleton lock implicitly...
			return ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
		}
	}
	return this;
}
 
Example 3
@Override
protected Object getResourceToInject(Object target, @Nullable String requestingBeanName) {
	if (StringUtils.hasLength(this.beanName)) {
		if (beanFactory != null && beanFactory.containsBean(this.beanName)) {
			// Local match found for explicitly specified local bean name.
			Object bean = beanFactory.getBean(this.beanName, this.lookupType);
			if (requestingBeanName != null && beanFactory instanceof ConfigurableBeanFactory) {
				((ConfigurableBeanFactory) beanFactory).registerDependentBean(this.beanName, requestingBeanName);
			}
			return bean;
		}
		else if (this.isDefaultName && !StringUtils.hasLength(this.mappedName)) {
			throw new NoSuchBeanDefinitionException(this.beanName,
					"Cannot resolve 'beanName' in local BeanFactory. Consider specifying a general 'name' value instead.");
		}
	}
	// JNDI name lookup - may still go to a local BeanFactory.
	return getResource(this, requestingBeanName);
}
 
Example 4
Source Project: spring4-understanding   Source File: AbstractBeanFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
	Assert.notNull(otherFactory, "BeanFactory must not be null");
	setBeanClassLoader(otherFactory.getBeanClassLoader());
	setCacheBeanMetadata(otherFactory.isCacheBeanMetadata());
	setBeanExpressionResolver(otherFactory.getBeanExpressionResolver());
	if (otherFactory instanceof AbstractBeanFactory) {
		AbstractBeanFactory otherAbstractFactory = (AbstractBeanFactory) otherFactory;
		this.customEditors.putAll(otherAbstractFactory.customEditors);
		this.propertyEditorRegistrars.addAll(otherAbstractFactory.propertyEditorRegistrars);
		this.beanPostProcessors.addAll(otherAbstractFactory.beanPostProcessors);
		this.hasInstantiationAwareBeanPostProcessors = this.hasInstantiationAwareBeanPostProcessors ||
				otherAbstractFactory.hasInstantiationAwareBeanPostProcessors;
		this.hasDestructionAwareBeanPostProcessors = this.hasDestructionAwareBeanPostProcessors ||
				otherAbstractFactory.hasDestructionAwareBeanPostProcessors;
		this.scopes.putAll(otherAbstractFactory.scopes);
		this.securityContextProvider = otherAbstractFactory.securityContextProvider;
	}
	else {
		setTypeConverter(otherFactory.getTypeConverter());
	}
}
 
Example 5
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
	ConfigurableBeanFactory cbf = (this.beanFactory instanceof ConfigurableBeanFactory ?
			(ConfigurableBeanFactory) this.beanFactory : null);

	// Annotation-based argument resolution
	resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, cbf));
	resolvers.add(new HeadersMethodArgumentResolver());

	// Type-based argument resolution
	resolvers.add(new MessageMethodArgumentResolver(this.messageConverter));

	if (this.customArgumentResolvers != null) {
		resolvers.addAll(this.customArgumentResolvers);
	}
	resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator));

	return resolvers;
}
 
Example 6
Source Project: lams   Source File: ParserStrategyUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * if implemented by the given object.
 */
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
		ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {

	if (parserStrategyBean instanceof Aware) {
		if (parserStrategyBean instanceof BeanClassLoaderAware) {
			ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
					((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
			((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
		}
		if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
			((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
		}
		if (parserStrategyBean instanceof EnvironmentAware) {
			((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
		}
		if (parserStrategyBean instanceof ResourceLoaderAware) {
			((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
		}
	}
}
 
Example 7
private String resolveName(String name) {
	if (!(this.beanFactory instanceof ConfigurableBeanFactory)) {
		return name;
	}

	ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) this.beanFactory;

	String placeholdersResolved = configurableBeanFactory.resolveEmbeddedValue(name);
	BeanExpressionResolver exprResolver = configurableBeanFactory
			.getBeanExpressionResolver();
	if (exprResolver == null) {
		return name;
	}
	Object result = exprResolver.evaluate(placeholdersResolved,
			new BeanExpressionContext(configurableBeanFactory, null));
	return result != null ? result.toString() : name;
}
 
Example 8
Source Project: lams   Source File: AbstractBeanFactory.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		return (beanInstance instanceof FactoryBean);
	}
	else if (containsSingleton(beanName)) {
		// null instance registered
		return false;
	}

	// No singleton instance found -> check bean definition.
	if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
		// No bean definition found in this factory -> delegate to parent.
		return ((ConfigurableBeanFactory) getParentBeanFactory()).isFactoryBean(name);
	}

	return isFactoryBean(beanName, getMergedLocalBeanDefinition(beanName));
}
 
Example 9
/**
 * Constructor with a {@link ConversionService} and a {@link BeanFactory}.
 * @param conversionService conversion service for converting String values
 * to the target method parameter type
 * @param beanFactory a bean factory for resolving {@code ${...}}
 * placeholders and {@code #{...}} SpEL expressions in default values
 */
protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionService,
		@Nullable ConfigurableBeanFactory beanFactory) {

	this.conversionService = conversionService;
	this.configurableBeanFactory = beanFactory;
	this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
}
 
Example 10
@Override
public void setBeanFactory(final BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		// Not using EmbeddedValueResolverAware in order to avoid a spring-context dependency:
		// ConfigurableBeanFactory and its resolveEmbeddedValue live in the spring-beans module.
		this.embeddedValueResolver = new StringValueResolver() {
			@Override
			public String resolveStringValue(String strVal) {
				return ((ConfigurableBeanFactory) beanFactory).resolveEmbeddedValue(strVal);
			}
		};
	}
}
 
Example 11
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public TableauTdeManager tableauTdeManager(){
    logger.trace("Creating an instance of " + TableauTdeManager.class.getSimpleName());

    return new TableauTdeManager();
}
 
Example 12
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "NLPImplementation")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public NLP NLP() {
    logger.trace("Creating an instance of NLPImplementation");

    return new NLP();
}
 
Example 13
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public OnDiskMinHandling onDiskMinHandling(){
    logger.trace("Creating an instance of " + OnDiskMinHandling.class.getSimpleName());

    return new OnDiskMinHandling();
}
 
Example 14
/**
 * Build an internal BeanFactory for resolving target beans.
 * @param containingFactory the containing BeanFactory that originally defines the beans
 * @return an independent internal BeanFactory to hold copies of some target beans
 */
protected DefaultListableBeanFactory buildInternalBeanFactory(ConfigurableBeanFactory containingFactory) {
	// Set parent so that references (up container hierarchies) are correctly resolved.
	DefaultListableBeanFactory internalBeanFactory = new DefaultListableBeanFactory(containingFactory);

	// Required so that all BeanPostProcessors, Scopes, etc become available.
	internalBeanFactory.copyConfigurationFrom(containingFactory);

	// Filter out BeanPostProcessors that are part of the AOP infrastructure,
	// since those are only meant to apply to beans defined in the original factory.
	internalBeanFactory.getBeanPostProcessors().removeIf(beanPostProcessor ->
			beanPostProcessor instanceof AopInfrastructureBean);

	return internalBeanFactory;
}
 
Example 15
/**
 * A {@link BeanFactory} only needs to be available in conjunction with
 * {@link #setContainerFactoryBeanName}.
 */
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	this.beanFactory = beanFactory;
	if (beanFactory instanceof ConfigurableBeanFactory) {
		ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
		this.mutex = cbf.getSingletonMutex();
	}
}
 
Example 16
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "RequestHandlingBean")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RequestHandlingBean requestHandlingBean() { // stateless/stateful bean
    logger.trace("Creating an instance of RequestHandlingBean");

    return new RequestHandlingBean();
}
 
Example 17
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "ClusterMessaging")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public ClusterMessaging clusterMessaging() { // stateless/stateful bean
    logger.trace("Creating an instance of ClusterMessaging");

    return new ClusterMessaging();
}
 
Example 18
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	if (this.owner instanceof ConfigurableBeanFactory) {
		ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
		enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
	}
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example 19
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public BSqlMemoryManagerOld memoryManagerOld() { // stateless/stateful bean
    logger.trace("Creating an instance of BSqlMemoryManagerOld");

    return new BSqlMemoryManagerOld();
}
 
Example 20
Source Project: halyard   Source File: ResourceConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) // snake yaml is not thread safe
// (https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-threading)
Yaml yamlParser() {
  DumperOptions options = new DumperOptions();
  options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
  return new Yaml(new SafeConstructor(), new Representer(), options);
}
 
Example 21
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "IntType")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public IntConverter intConverter() { // stateless/stateful factory patterned bean
    logger.trace("Creating an instance of IntConverter");

    return new IntConverter();
}
 
Example 22
Source Project: syndesis   Source File: Application.java    License: Apache License 2.0 5 votes vote down vote up
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@Bean(name = "verifier-context", initMethod = "start", destroyMethod = "stop")
public static CamelContext verifierContext() {
    CamelContext context = new DefaultCamelContext();
    context.setNameStrategy(new ExplicitCamelContextNameStrategy("verifier-context"));
    context.disableJMX();

    return context;
}
 
Example 23
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
protected TestConfiguration createTestConfiguration(final WebApplicationContext context, final HttpSession httpSession) {
    Assert.requireNonNull(context, "context");
    try {
        return new TestConfiguration(context, httpSession);
    } catch (Exception e) {
        throw new RuntimeException("Can not create test configuration", e);
    }
}
 
Example 24
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public TriggerExecutorBean triggerExecutorBean() {
    logger.trace("creating an instance of TriggerExecutorBean");

    return new TriggerExecutorBean();
}
 
Example 25
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "Version1to2")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Version1to2 version1to2() { // stateless/stateful bean
    logger.trace("Creating an instance of Version1to2");

    return new Version1to2();
}
 
Example 26
Source Project: db   Source File: EngineBeanConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public InternalQueryBean internalQueryBean(){
    logger.trace("Creating an instance of " + InternalQueryBean.class.getSimpleName());

    return new InternalQueryBean();
}
 
Example 27
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	if (this.owner instanceof ConfigurableBeanFactory) {
		ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
		enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
	}
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example 28
Source Project: java-technology-stack   Source File: AspectJExpressionPointcut.java    License: MIT License 5 votes vote down vote up
/**
 * Determine the ClassLoader to use for pointcut evaluation.
 */
@Nullable
private ClassLoader determinePointcutClassLoader() {
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
	}
	if (this.pointcutDeclarationScope != null) {
		return this.pointcutDeclarationScope.getClassLoader();
	}
	return ClassUtils.getDefaultClassLoader();
}
 
Example 29
/**
 * Find an EntityManagerFactory with the given name in the current
 * Spring application context.
 * @param unitName the name of the persistence unit (never empty)
 * @param requestingBeanName the name of the requesting bean
 * @return the EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context
 */
protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, @Nullable String requestingBeanName)
		throws NoSuchBeanDefinitionException {

	Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup");

	EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName);
	if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory) {
		((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName);
	}
	return emf;
}
 
Example 30
Source Project: blog_demos   Source File: AbstractBeanFactory.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return a 'merged' BeanDefinition for the given bean name,
 * merging a child bean definition with its parent if necessary.
 * <p>This {@code getMergedBeanDefinition} considers bean definition
 * in ancestors as well.
 * @param name the name of the bean to retrieve the merged definition for
 * (may be an alias)
 * @return a (potentially merged) RootBeanDefinition for the given bean
 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
 * @throws BeanDefinitionStoreException in case of an invalid bean definition
 */
@Override
public BeanDefinition getMergedBeanDefinition(String name) throws BeansException {
	String beanName = transformedBeanName(name);

	// Efficiently check whether bean definition exists in this factory.
	if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
		return ((ConfigurableBeanFactory) getParentBeanFactory()).getMergedBeanDefinition(beanName);
	}
	// Resolve merged bean definition locally.
	return getMergedLocalBeanDefinition(beanName);
}