Search in sources :

Example 1 with BeanDefinitionRegistry

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

the class ConditionalTests method testConditionalOnResource.

@Test
public void testConditionalOnResource() throws Exception {
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        ApplicationPropertySourcesProcessor processor = new ApplicationPropertySourcesProcessor(context);
        processor.setPropertiesLocation("info.properties");
        processor.postProcessEnvironment();
        context.scan("cn.taketoday.context.condition");
        BeanDefinitionRegistry registry = context.unwrapFactory(BeanDefinitionRegistry.class);
        Assertions.assertFalse(registry.containsBeanDefinition(ConditionalOnResource_.class));
        Assertions.assertTrue(registry.containsBeanDefinition(ConditionalOnResource__.class));
        Assertions.assertTrue(registry.containsBeanDefinition(ConditionalOnResource___.class));
        Assertions.assertTrue(registry.containsBeanDefinition(ConditionalOnResource____.class));
    }
}
Also used : ApplicationPropertySourcesProcessor(cn.taketoday.context.support.ApplicationPropertySourcesProcessor) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with BeanDefinitionRegistry

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

the class AbstractServletWebServer method prepareInitialize.

@Override
protected void prepareInitialize() {
    super.prepareInitialize();
    WebServerApplicationContext context = obtainApplicationContext();
    Class<?> mainApplicationClass = getMainApplicationClass();
    if (mainApplicationClass != null) {
        ServletSecurity servletSecurity = mainApplicationClass.getAnnotation(ServletSecurity.class);
        if (servletSecurity != null) {
            BeanDefinitionRegistry registry = context.unwrapFactory(BeanDefinitionRegistry.class);
            if (registry.containsBeanDefinition(ServletSecurityElement.class)) {
                log.info("Multiple: [{}] Overriding its bean definition", ServletSecurityElement.class.getName());
            }
            context.unwrap(BeanDefinitionRegistrar.class).registerSingleton(new ServletSecurityElement(servletSecurity));
        }
    }
    addDefaultServlet();
    addJspServlet();
}
Also used : WebServerApplicationContext(cn.taketoday.web.framework.WebServerApplicationContext) ServletSecurity(jakarta.servlet.annotation.ServletSecurity) BeanDefinitionRegistrar(cn.taketoday.context.loader.BeanDefinitionRegistrar) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) ServletSecurityElement(jakarta.servlet.ServletSecurityElement)

Example 3 with BeanDefinitionRegistry

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

the class ClassPathMapperScanner method postProcessBeanDefinition.

protected void postProcessBeanDefinition(BeanDefinitionHolder holder) {
    AbstractBeanDefinition definition = (AbstractBeanDefinition) holder.getBeanDefinition();
    String beanClassName = definition.getBeanClassName();
    log.debug("Creating MapperFactoryBean with name '{}' and '{}' mapperInterface", definition.getBeanName(), beanClassName);
    // the mapper interface is the original class of the bean
    // but, the actual class of the bean is MapperFactoryBean
    // issue #59
    definition.getConstructorArgumentValues().addGenericArgumentValue(beanClassName);
    definition.setBeanClass(mapperFactoryBeanClass);
    PropertyValues propertyValues = definition.getPropertyValues();
    propertyValues.add("addToConfig", addToConfig);
    // Attribute for MockitoPostProcessor
    // https://github.com/mybatis/spring-boot-starter/issues/475
    definition.setAttribute(FACTORY_BEAN_OBJECT_TYPE, beanClassName);
    boolean explicitFactoryUsed = false;
    if (StringUtils.hasText(sqlSessionFactoryBeanName)) {
        propertyValues.add("sqlSessionFactory", RuntimeBeanReference.from(sqlSessionFactoryBeanName));
        explicitFactoryUsed = true;
    } else if (sqlSessionFactory != null) {
        propertyValues.add("sqlSessionFactory", sqlSessionFactory);
        explicitFactoryUsed = true;
    }
    if (StringUtils.hasText(sqlSessionTemplateBeanName)) {
        if (explicitFactoryUsed) {
            log.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        propertyValues.add("sqlSessionTemplate", RuntimeBeanReference.from(sqlSessionTemplateBeanName));
        explicitFactoryUsed = true;
    } else if (sqlSessionTemplate != null) {
        if (explicitFactoryUsed) {
            log.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        propertyValues.add("sqlSessionTemplate", sqlSessionTemplate);
        explicitFactoryUsed = true;
    }
    if (!explicitFactoryUsed) {
        log.debug("Enabling autowire by type for MapperFactoryBean with name '{}'.", definition.getBeanName());
        definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    }
    definition.setLazyInit(lazyInitialization);
    if (BeanDefinition.SCOPE_SINGLETON.equals(definition.getScope()) && defaultScope != null) {
        definition.setScope(defaultScope);
    }
    if (!definition.isSingleton()) {
        BeanDefinitionRegistry registry = delegate.getRegistry();
        BeanDefinitionHolder proxyHolder = ScopedProxyUtils.createScopedProxy(holder, registry, true);
        if (registry.containsBeanDefinition(proxyHolder.getBeanName())) {
            registry.removeBeanDefinition(proxyHolder.getBeanName());
        }
        registry.registerBeanDefinition(proxyHolder.getBeanName(), proxyHolder.getBeanDefinition());
    }
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry)

Example 4 with BeanDefinitionRegistry

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

the class AnnotationBeanNamePopulatorTests method generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName.

@Test
public void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() {
    BeanDefinitionRegistry registry = new DefaultBeanDefinitionRegistry();
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnonymousComponent.class);
    String beanName = this.beanNamePopulator.populateName(bd, registry);
    assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
    assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
    String expectedGeneratedBeanName = this.beanNamePopulator.buildDefaultBeanName(bd);
    assertThat(beanName).isEqualTo(expectedGeneratedBeanName);
}
Also used : AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) DefaultBeanDefinitionRegistry(cn.taketoday.beans.factory.support.DefaultBeanDefinitionRegistry) DefaultBeanDefinitionRegistry(cn.taketoday.beans.factory.support.DefaultBeanDefinitionRegistry) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with BeanDefinitionRegistry

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

the class AnnotationBeanNamePopulatorTests method generateBeanNameFromComposedControllerAnnotationWithBlankName.

@Test
public void generateBeanNameFromComposedControllerAnnotationWithBlankName() {
    // SPR-11360
    BeanDefinitionRegistry registry = new DefaultBeanDefinitionRegistry();
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithBlankName.class);
    String beanName = this.beanNamePopulator.populateName(bd, registry);
    String expectedGeneratedBeanName = this.beanNamePopulator.buildDefaultBeanName(bd);
    assertThat(beanName).isEqualTo(expectedGeneratedBeanName);
}
Also used : AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) DefaultBeanDefinitionRegistry(cn.taketoday.beans.factory.support.DefaultBeanDefinitionRegistry) DefaultBeanDefinitionRegistry(cn.taketoday.beans.factory.support.DefaultBeanDefinitionRegistry) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)26 Test (org.junit.jupiter.api.Test)15 AnnotatedBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)10 AnnotatedGenericBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition)9 DefaultBeanDefinitionRegistry (cn.taketoday.beans.factory.support.DefaultBeanDefinitionRegistry)9 ApplicationPropertySourcesProcessor (cn.taketoday.context.support.ApplicationPropertySourcesProcessor)5 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)5 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)4 BeanDefinitionHolder (cn.taketoday.beans.factory.config.BeanDefinitionHolder)2 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)2 SuppliedMethodInterceptor (cn.taketoday.aop.support.SuppliedMethodInterceptor)1 BeanInstantiationException (cn.taketoday.beans.BeanInstantiationException)1 PropertyValues (cn.taketoday.beans.PropertyValues)1 BeanFactory (cn.taketoday.beans.factory.BeanFactory)1 AutowireCapableBeanFactory (cn.taketoday.beans.factory.config.AutowireCapableBeanFactory)1 ConfigurableBeanFactory (cn.taketoday.beans.factory.config.ConfigurableBeanFactory)1 CompositeComponentDefinition (cn.taketoday.beans.factory.parsing.CompositeComponentDefinition)1 BeanDefinitionBuilder (cn.taketoday.beans.factory.support.BeanDefinitionBuilder)1 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)1 AnnotatedBeanDefinitionReader (cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)1