Search in sources :

Example 56 with BeanFactory

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

the class BeanFactoryDataSourceLookupTests method testLookupWhereBeanFactoryYieldsNonDataSourceType.

@Test
public void testLookupWhereBeanFactoryYieldsNonDataSourceType() throws Exception {
    final BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.getBean(DATASOURCE_BEAN_NAME, DataSource.class)).willThrow(new BeanNotOfRequiredTypeException(DATASOURCE_BEAN_NAME, DataSource.class, String.class));
    BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory);
    assertThatExceptionOfType(DataSourceLookupFailureException.class).isThrownBy(() -> lookup.getDataSource(DATASOURCE_BEAN_NAME));
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 57 with BeanFactory

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

the class AbstractBeanFactoryTests method aliasing.

// TODO: refactor in AbstractBeanFactory (tests for AbstractBeanFactory)
// and rename this class
@Test
public void aliasing() {
    BeanFactory bf = getBeanFactory();
    if (!(bf instanceof ConfigurableBeanFactory)) {
        return;
    }
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) bf;
    String alias = "rods alias";
    try {
        cbf.getBean(alias);
        fail("Shouldn't permit factory get on normal bean");
    } catch (NoSuchBeanDefinitionException ex) {
        // Ok
        assertTrue(alias.equals(ex.getBeanName()));
    }
    // Create alias
    cbf.registerAlias("rod", alias);
    Object rod = getBeanFactory().getBean("rod");
    Object aliasRod = getBeanFactory().getBean(alias);
    assertTrue(rod == aliasRod);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 58 with BeanFactory

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

the class DefaultListableBeanFactory method checkBeanNotOfRequiredType.

/**
	 * Raise a BeanNotOfRequiredTypeException for an unresolvable dependency, if applicable,
	 * i.e. if the target type of the bean would match but an exposed proxy doesn't.
	 */
private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor descriptor) {
    for (String beanName : this.beanDefinitionNames) {
        RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
        Class<?> targetType = mbd.getTargetType();
        if (targetType != null && type.isAssignableFrom(targetType) && isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) {
            // Probably a proxy interfering with target type match -> throw meaningful exception.
            Object beanInstance = getSingleton(beanName, false);
            Class<?> beanType = (beanInstance != null ? beanInstance.getClass() : predictBeanType(beanName, mbd));
            if (!type.isAssignableFrom((beanType))) {
                throw new BeanNotOfRequiredTypeException(beanName, type, beanType);
            }
        }
    }
    BeanFactory parent = getParentBeanFactory();
    if (parent instanceof DefaultListableBeanFactory) {
        ((DefaultListableBeanFactory) parent).checkBeanNotOfRequiredType(type, descriptor);
    }
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException)

Example 59 with BeanFactory

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

the class DefaultListableBeanFactory method getBean.

@Override
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
    NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args);
    if (namedBean != null) {
        return namedBean.getBeanInstance();
    }
    BeanFactory parent = getParentBeanFactory();
    if (parent != null) {
        return parent.getBean(requiredType, args);
    }
    throw new NoSuchBeanDefinitionException(requiredType);
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 60 with BeanFactory

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

the class AbstractAutowireCapableBeanFactory method autowire.

@Override
public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
    // Use non-singleton bean definition, to avoid registering bean as dependent bean.
    final RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    if (bd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR) {
        return autowireConstructor(beanClass.getName(), bd, null, null).getWrappedInstance();
    } else {
        Object bean;
        final BeanFactory parent = this;
        if (System.getSecurityManager() != null) {
            bean = AccessController.doPrivileged(new PrivilegedAction<Object>() {

                @Override
                public Object run() {
                    return getInstantiationStrategy().instantiate(bd, null, parent);
                }
            }, getAccessControlContext());
        } else {
            bean = getInstantiationStrategy().instantiate(bd, null, parent);
        }
        populateBean(beanClass.getName(), bd, new BeanWrapperImpl(bean));
        return bean;
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PrivilegedAction(java.security.PrivilegedAction) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)121 Test (org.junit.jupiter.api.Test)30 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)25 Test (org.junit.Test)20 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)16 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)15 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutorService (java.util.concurrent.ExecutorService)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)4 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)4