Search in sources :

Example 76 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

the class ControllerAdviceBeanTests method equalsHashCodeAndToStringForBeanName.

@Test
public void equalsHashCodeAndToStringForBeanName() {
    String beanName = "myBean";
    BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.containsBean(beanName)).willReturn(true);
    ControllerAdviceBean bean1 = new ControllerAdviceBean(beanName, beanFactory);
    ControllerAdviceBean bean2 = new ControllerAdviceBean(beanName, beanFactory);
    assertEqualsHashCodeAndToString(bean1, bean2, beanName);
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test)

Example 77 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.factory.BeanFactory) BeanNotOfRequiredTypeException(cn.taketoday.beans.factory.BeanNotOfRequiredTypeException) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 78 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class ConfigurationPropertiesBinder method register.

static void register(BeanDefinitionRegistry registry) {
    if (!registry.containsBeanDefinition(FACTORY_BEAN_NAME)) {
        BeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(ConfigurationPropertiesBinder.Factory.class).getBeanDefinition();
        definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(ConfigurationPropertiesBinder.FACTORY_BEAN_NAME, definition);
    }
    if (!registry.containsBeanDefinition(BEAN_NAME)) {
        BeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(ConfigurationPropertiesBinder.class, () -> ((BeanFactory) registry).getBean(FACTORY_BEAN_NAME, ConfigurationPropertiesBinder.Factory.class).create()).getBeanDefinition();
        definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(ConfigurationPropertiesBinder.BEAN_NAME, definition);
    }
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition)

Example 79 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class BeanNameAutoProxyCreator method isSupportedBeanName.

/**
 * Determine if the bean name for the given bean class matches one of the names
 * in the configured list of supported names.
 *
 * @param beanClass the class of the bean to advise
 * @param beanName the name of the bean
 * @return {@code true} if the given bean name is supported
 * @see #setBeanNames(String...)
 */
private boolean isSupportedBeanName(Class<?> beanClass, String beanName) {
    if (this.beanNames != null) {
        boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
        for (String mappedName : this.beanNames) {
            if (isFactoryBean) {
                if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
                    continue;
                }
                mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
            }
            if (isMatch(beanName, mappedName)) {
                return true;
            }
        }
        BeanFactory beanFactory = getBeanFactory();
        String[] aliases = (beanFactory != null ? beanFactory.getAliases(beanName) : NO_ALIASES);
        for (String alias : aliases) {
            for (String mappedName : this.beanNames) {
                if (isMatch(alias, mappedName)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory)

Example 80 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class AspectAutoProxyCreator method getFactory.

@NonNull
private BeanFactory getFactory() {
    BeanFactory beanFactory = getBeanFactory();
    Assert.state(beanFactory != null, "No BeanFactory available");
    return beanFactory;
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) NonNull(cn.taketoday.lang.NonNull)

Aggregations

BeanFactory (cn.taketoday.beans.factory.BeanFactory)80 Test (org.junit.jupiter.api.Test)60 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)24 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)16 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)16 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)8 CallCountingTransactionManager (cn.taketoday.testfixture.transaction.CallCountingTransactionManager)8 TransactionManager (cn.taketoday.transaction.TransactionManager)8 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)6 List (java.util.List)6 DataSource (javax.sql.DataSource)6 Advised (cn.taketoday.aop.framework.Advised)4 BeansException (cn.taketoday.beans.BeansException)4 FatalBeanException (cn.taketoday.beans.FatalBeanException)4 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)4 Nullable (cn.taketoday.lang.Nullable)4 Collection (java.util.Collection)4 Set (java.util.Set)4 ServletException (jakarta.servlet.ServletException)3