Search in sources :

Example 1 with FactoryBean

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

the class AbstractBeanFactory method isTypeMatch.

@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
    String beanName = transformedBeanName(name);
    // Check manually registered singletons.
    Object beanInstance = getSingleton(beanName, false);
    if (beanInstance != null) {
        if (beanInstance instanceof FactoryBean) {
            if (!BeanFactoryUtils.isFactoryDereference(name)) {
                Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance);
                return (type != null && typeToMatch.isAssignableFrom(type));
            } else {
                return typeToMatch.isInstance(beanInstance);
            }
        } else if (!BeanFactoryUtils.isFactoryDereference(name)) {
            if (typeToMatch.isInstance(beanInstance)) {
                // Direct match for exposed instance?
                return true;
            } else if (typeToMatch.hasGenerics() && containsBeanDefinition(beanName)) {
                // Generics potentially only match on the target class, not on the proxy...
                RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                Class<?> targetType = mbd.getTargetType();
                if (targetType != null && targetType != ClassUtils.getUserClass(beanInstance) && typeToMatch.isAssignableFrom(targetType)) {
                    // Check raw class match as well, making sure it's exposed on the proxy.
                    Class<?> classToMatch = typeToMatch.resolve();
                    return (classToMatch == null || classToMatch.isInstance(beanInstance));
                }
            }
        }
        return false;
    } else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
        // null instance registered
        return false;
    }
    // No singleton instance found -> check bean definition.
    BeanFactory parentBeanFactory = getParentBeanFactory();
    if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
        // No bean definition found in this factory -> delegate to parent.
        return parentBeanFactory.isTypeMatch(originalBeanName(name), typeToMatch);
    }
    // Retrieve corresponding bean definition.
    RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
    Class<?> classToMatch = typeToMatch.resolve();
    if (classToMatch == null) {
        classToMatch = FactoryBean.class;
    }
    Class<?>[] typesToMatch = (FactoryBean.class == classToMatch ? new Class<?>[] { classToMatch } : new Class<?>[] { FactoryBean.class, classToMatch });
    // Check decorated bean definition, if any: We assume it'll be easier
    // to determine the decorated bean's type than the proxy's type.
    BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
    if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
        RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
        Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd, typesToMatch);
        if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
            return typeToMatch.isAssignableFrom(targetClass);
        }
    }
    Class<?> beanType = predictBeanType(beanName, mbd, typesToMatch);
    if (beanType == null) {
        return false;
    }
    // Check bean class whether we're dealing with a FactoryBean.
    if (FactoryBean.class.isAssignableFrom(beanType)) {
        if (!BeanFactoryUtils.isFactoryDereference(name)) {
            // If it's a FactoryBean, we want to look at what it creates, not the factory class.
            beanType = getTypeForFactoryBean(beanName, mbd);
            if (beanType == null) {
                return false;
            }
        }
    } else if (BeanFactoryUtils.isFactoryDereference(name)) {
        // Special case: A SmartInstantiationAwareBeanPostProcessor returned a non-FactoryBean
        // type but we nevertheless are being asked to dereference a FactoryBean...
        // Let's check the original bean class and proceed with it if it is a FactoryBean.
        beanType = predictBeanType(beanName, mbd, FactoryBean.class);
        if (beanType == null || !FactoryBean.class.isAssignableFrom(beanType)) {
            return false;
        }
    }
    ResolvableType resolvableType = mbd.targetType;
    if (resolvableType == null) {
        resolvableType = mbd.factoryMethodReturnType;
    }
    if (resolvableType != null && resolvableType.resolve() == beanType) {
        return typeToMatch.isAssignableFrom(resolvableType);
    }
    return typeToMatch.isAssignableFrom(beanType);
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) ResolvableType(org.springframework.core.ResolvableType)

Example 2 with FactoryBean

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

the class DefaultListableBeanFactory method preInstantiateSingletons.

@Override
public void preInstantiateSingletons() throws BeansException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Pre-instantiating singletons in " + this);
    }
    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
    // Trigger initialization of all non-lazy singleton beans...
    for (String beanName : beanNames) {
        RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
        if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            if (isFactoryBean(beanName)) {
                final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
                boolean isEagerInit;
                if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                    isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

                        @Override
                        public Boolean run() {
                            return ((SmartFactoryBean<?>) factory).isEagerInit();
                        }
                    }, getAccessControlContext());
                } else {
                    isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit());
                }
                if (isEagerInit) {
                    getBean(beanName);
                }
            } else {
                getBean(beanName);
            }
        }
    }
    // Trigger post-initialization callback for all applicable beans...
    for (String beanName : beanNames) {
        Object singletonInstance = getSingleton(beanName);
        if (singletonInstance instanceof SmartInitializingSingleton) {
            final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {

                    @Override
                    public Object run() {
                        smartSingleton.afterSingletonsInstantiated();
                        return null;
                    }
                }, getAccessControlContext());
            } else {
                smartSingleton.afterSingletonsInstantiated();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) SmartInitializingSingleton(org.springframework.beans.factory.SmartInitializingSingleton) PrivilegedAction(java.security.PrivilegedAction) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean)

Example 3 with FactoryBean

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

the class ConfigurationClassSpr8954Tests method repro.

@Test
public void repro() {
    AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
    bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
    bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
    bf.refresh();
    assertThat(bf.getBean("foo"), instanceOf(Foo.class));
    assertThat(bf.getBean("&foo"), instanceOf(FooFactoryBean.class));
    assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true));
    @SuppressWarnings("rawtypes") Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
    assertThat(1, equalTo(fbBeans.size()));
    assertThat("&foo", equalTo(fbBeans.keySet().iterator().next()));
    Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
    assertThat(1, equalTo(aiBeans.size()));
    assertThat("&foo", equalTo(aiBeans.keySet().iterator().next()));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.Test)

Example 4 with FactoryBean

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

the class ConfigurationClassSpr8954Tests method findsBeansByTypeIfNotInstantiated.

@Test
public void findsBeansByTypeIfNotInstantiated() {
    AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
    bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
    bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
    bf.refresh();
    assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true));
    @SuppressWarnings("rawtypes") Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
    assertThat(1, equalTo(fbBeans.size()));
    assertThat("&foo", equalTo(fbBeans.keySet().iterator().next()));
    Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
    assertThat(1, equalTo(aiBeans.size()));
    assertThat("&foo", equalTo(aiBeans.keySet().iterator().next()));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.Test)

Example 5 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project mule by mulesoft.

the class DefaultListableBeanFactory method preInstantiateSingletons.

@Override
public void preInstantiateSingletons() throws BeansException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Pre-instantiating singletons in " + this);
    }
    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
    List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);
    // Trigger initialization of all non-lazy singleton beans...
    for (String beanName : beanNames) {
        RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
        if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            if (isFactoryBean(beanName)) {
                final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
                boolean isEagerInit;
                if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                    isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

                        @Override
                        public Boolean run() {
                            return ((SmartFactoryBean<?>) factory).isEagerInit();
                        }
                    }, getAccessControlContext());
                } else {
                    isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit());
                }
                if (isEagerInit) {
                    getBean(beanName);
                }
            } else {
                getBean(beanName);
            }
        }
    }
    // Trigger post-initialization callback for all applicable beans...
    for (String beanName : beanNames) {
        Object singletonInstance = getSingleton(beanName);
        if (singletonInstance instanceof SmartInitializingSingleton) {
            final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {

                    @Override
                    public Object run() {
                        smartSingleton.afterSingletonsInstantiated();
                        return null;
                    }
                }, getAccessControlContext());
            } else {
                smartSingleton.afterSingletonsInstantiated();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) SmartInitializingSingleton(org.springframework.beans.factory.SmartInitializingSingleton) PrivilegedAction(java.security.PrivilegedAction) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean)

Aggregations

FactoryBean (org.springframework.beans.factory.FactoryBean)28 Test (org.junit.jupiter.api.Test)10 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)5 Bean (org.springframework.context.annotation.Bean)5 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)4 BeansException (org.springframework.beans.BeansException)3 GroovyObject (groovy.lang.GroovyObject)2 PrivilegedAction (java.security.PrivilegedAction)2 ArrayList (java.util.ArrayList)2 HttpClientProperties (org.apereo.cas.configuration.model.core.authentication.HttpClientProperties)2 Test (org.junit.Test)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)2