Search in sources :

Example 1 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class AbstractAutoProxyCreator method resolveInterceptorNames.

/**
	 * Resolves the specified interceptor names to Advisor objects.
	 * @see #setInterceptorNames
	 */
private Advisor[] resolveInterceptorNames() {
    ConfigurableBeanFactory cbf = (this.beanFactory instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) this.beanFactory : null);
    List<Advisor> advisors = new ArrayList<>();
    for (String beanName : this.interceptorNames) {
        if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) {
            Object next = this.beanFactory.getBean(beanName);
            advisors.add(this.advisorAdapterRegistry.wrap(next));
        }
    }
    return advisors.toArray(new Advisor[advisors.size()]);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor)

Example 2 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class ScopedProxyFactoryBean method setBeanFactory.

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    if (!(beanFactory instanceof ConfigurableBeanFactory)) {
        throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
    }
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
    this.scopedTargetSource.setBeanFactory(beanFactory);
    ProxyFactory pf = new ProxyFactory();
    pf.copyFrom(this);
    pf.setTargetSource(this.scopedTargetSource);
    Class<?> beanType = beanFactory.getType(this.targetBeanName);
    if (beanType == null) {
        throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName + "': Target type could not be determined at the time of proxy creation.");
    }
    if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
        pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));
    }
    // Add an introduction that implements only the methods on ScopedObject.
    ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
    pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));
    // Add the AopInfrastructureBean marker to indicate that the scoped proxy
    // itself is not subject to auto-proxying! Only its target bean is.
    pf.addInterface(AopInfrastructureBean.class);
    this.proxy = pf.getProxy(cbf.getBeanClassLoader());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 3 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessor method setBeanFactory.

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    Assert.notNull(beanFactory, "BeanFactory must not be null");
    this.beanFactory = beanFactory;
    if (this.resourceFactory == null) {
        this.resourceFactory = beanFactory;
    }
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory) beanFactory);
    }
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) EmbeddedValueResolver(org.springframework.beans.factory.config.EmbeddedValueResolver)

Example 4 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessor method buildLazyResourceProxy.

/**
	 * Obtain a lazily resolving resource proxy for the given name and type,
	 * delegating to {@link #getResource} on demand once a method call comes in.
	 * @param element the descriptor for the annotated field/method
	 * @param requestingBeanName the name of the requesting bean
	 * @return the resource object (never {@code null})
	 * @since 4.2
	 * @see #getResource
	 * @see Lazy
	 */
protected Object buildLazyResourceProxy(final LookupElement element, final String requestingBeanName) {
    TargetSource ts = new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return element.lookupType;
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() {
            return getResource(element, requestingBeanName);
        }

        @Override
        public void releaseTarget(Object target) {
        }
    };
    ProxyFactory pf = new ProxyFactory();
    pf.setTargetSource(ts);
    if (element.lookupType.isInterface()) {
        pf.addInterface(element.lookupType);
    }
    ClassLoader classLoader = (this.beanFactory instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() : null);
    return pf.getProxy(classLoader);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) TargetSource(org.springframework.aop.TargetSource) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 5 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessor method autowireResource.

/**
	 * Obtain a resource object for the given name and type through autowiring
	 * based on the given factory.
	 * @param factory the factory to autowire against
	 * @param element the descriptor for the annotated field/method
	 * @param requestingBeanName the name of the requesting bean
	 * @return the resource object (never {@code null})
	 * @throws BeansException if we failed to obtain the target resource
	 */
protected Object autowireResource(BeanFactory factory, LookupElement element, String requestingBeanName) throws BeansException {
    Object resource;
    Set<String> autowiredBeanNames;
    String name = element.name;
    if (this.fallbackToDefaultTypeMatch && element.isDefaultName && factory instanceof AutowireCapableBeanFactory && !factory.containsBean(name)) {
        autowiredBeanNames = new LinkedHashSet<>();
        resource = ((AutowireCapableBeanFactory) factory).resolveDependency(element.getDependencyDescriptor(), requestingBeanName, autowiredBeanNames, null);
    } else {
        resource = factory.getBean(name, element.lookupType);
        autowiredBeanNames = Collections.singleton(name);
    }
    if (factory instanceof ConfigurableBeanFactory) {
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
        for (String autowiredBeanName : autowiredBeanNames) {
            if (beanFactory.containsBean(autowiredBeanName)) {
                beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
            }
        }
    }
    return resource;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory)

Aggregations

ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)18 ArrayList (java.util.ArrayList)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 PostConstruct (javax.annotation.PostConstruct)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 EmbeddedValueResolver (org.springframework.beans.factory.config.EmbeddedValueResolver)2 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 HandlerMethodArgumentResolver (org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 WebService (javax.jws.WebService)1 Endpoint (javax.xml.ws.Endpoint)1 WebServiceProvider (javax.xml.ws.WebServiceProvider)1 HystrixConfigurationCommon (org.apache.camel.model.HystrixConfigurationCommon)1