Search in sources :

Example 1 with AutowireCapableBeanFactory

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

the class ClassUtilsTests method testAutowiredOnConstructor.

@Test
void testAutowiredOnConstructor() {
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        context.refresh();
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        context.register(AutowiredOnConstructor.class, AutowiredOnConstructorThrow.class);
        assertThat(context.getBean(AutowiredOnConstructor.class)).isNotNull();
        context.registerBean("testAutowiredOnConstructorThrow", AutowiredOnConstructorThrow.class);
        try {
            context.getBean(AutowiredOnConstructorThrow.class);
            assert false;
        } catch (Exception e) {
            assert true;
        }
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) ApplicationContextException(cn.taketoday.context.ApplicationContextException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with AutowireCapableBeanFactory

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

the class AspectAutoProxyCreator method getInterceptor.

private MethodInterceptor getInterceptor(BeanDefinition aspectDef, @Nullable Method aspectMethod, MergedAnnotation<Advice> advice) {
    BeanFactory beanFactory = getFactory();
    if (aspectMethod == null) {
        // method interceptor
        if (!beanFactory.isTypeMatch(aspectDef.getBeanName(), MethodInterceptor.class)) {
            throw new ConfigurationException('[' + aspectDef.getBeanClassName() + "] must be implement: [" + MethodInterceptor.class.getName() + ']');
        }
        // aspect is a method interceptor
        return getMethodInterceptor(beanFactory, aspectDef);
    }
    // -----------------
    // invoke advice method that annotated: @AfterReturning @Around @Before @After @AfterThrowing
    Class<? extends MethodInterceptor> interceptor = advice.getClass("interceptor");
    if (interceptor == AbstractAnnotationMethodInterceptor.class || !MethodInterceptor.class.isAssignableFrom(interceptor)) {
        throw new ConfigurationException(interceptor + " must be implement: [" + AbstractAnnotationMethodInterceptor.class.getName() + "] or [" + MethodInterceptor.class.getName() + "]");
    }
    // exist in bean factory ?
    if (AnnotatedElementUtils.hasAnnotation(interceptor, Component.class)) {
        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinition interceptorDef = ((BeanDefinitionRegistry) beanFactory).getBeanDefinition(interceptor);
            if (interceptorDef != null) {
                // exist in bean factory
                return getMethodInterceptor(beanFactory, interceptorDef);
            }
        } else {
            // just get bean
            MethodInterceptor ret = beanFactory.getBean(interceptor);
            if (ret != null) {
                return ret;
            }
        }
    }
    // dynamic parameters -> aspectMethod, beanName, beanFactory
    MethodInterceptor ret = DependencyInjectorAwareInstantiator.instantiate(interceptor, beanFactory, new Object[] { aspectMethod, aspectDef.getBeanName(), beanFactory });
    if (beanFactory instanceof AutowireCapableBeanFactory) {
        ((AutowireCapableBeanFactory) beanFactory).autowireBean(ret);
    }
    return ret;
}
Also used : SuppliedMethodInterceptor(cn.taketoday.aop.support.SuppliedMethodInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ConfigurationException(cn.taketoday.core.ConfigurationException) BeanFactory(cn.taketoday.beans.factory.BeanFactory) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory)

Example 3 with AutowireCapableBeanFactory

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

the class AutowireCapableBeanFactoryTests method testCreateBean.

@Test
void testCreateBean() {
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        CreateTestBean bean = beanFactory.createBean(CreateTestBean.class);
        CreateTestBean bean2 = beanFactory.createBean(CreateTestBean.class);
        assertThat(bean.property).isZero();
        assertThat(bean2).isNotEqualTo(bean);
        CreateTestBean cachedBeanDef = beanFactory.createBean(CreateTestBean.class);
        CreateTestBean cachedBeanDef2 = beanFactory.createBean(CreateTestBean.class);
        assertThat(cachedBeanDef.property).isZero();
        assertThat(cachedBeanDef2).isNotEqualTo(cachedBeanDef);
        AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) context.getBeanDefinition(CreateTestBean.class);
        assertThat(beanDefinition.getBeanClass()).isEqualTo(CreateTestBean.class);
        assertThat(beanDefinition.getScope()).isEqualTo(Scope.PROTOTYPE);
        assertThat(beanDefinition.getPropertyValues()).isNull();
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 4 with AutowireCapableBeanFactory

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

the class ParameterResolutionDelegateTests method resolveDependencyForAnnotatedParametersInTopLevelClassConstructor.

@Test
public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception {
    Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);
    AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class);
    // Configure the mocked BeanFactory to return the DependencyDescriptor for convenience and
    // to avoid using an ArgumentCaptor.
    given(beanFactory.resolveDependency(any(), isNull())).willAnswer(invocation -> invocation.getArgument(0));
    Parameter[] parameters = constructor.getParameters();
    for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
        Parameter parameter = parameters[parameterIndex];
        DependencyDescriptor intermediateDependencyDescriptor = (DependencyDescriptor) ParameterResolutionDelegate.resolveDependency(parameter, parameterIndex, AutowirableClass.class, beanFactory);
        assertThat(intermediateDependencyDescriptor.getAnnotatedElement()).isEqualTo(constructor);
        assertThat(intermediateDependencyDescriptor.getMethodParameter().getParameter()).isEqualTo(parameter);
    }
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) Parameter(java.lang.reflect.Parameter) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 5 with AutowireCapableBeanFactory

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

the class ClassUtilsTests method testAutowiredOnConstructor.

@Test
void testAutowiredOnConstructor() {
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        context.refresh();
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        context.register(AutowiredOnConstructor.class, AutowiredOnConstructorThrow.class);
        assertThat(context.getBean(AutowiredOnConstructor.class)).isNotNull();
        context.registerBean("testAutowiredOnConstructorThrow", AutowiredOnConstructorThrow.class);
        try {
            context.getBean(AutowiredOnConstructorThrow.class);
            assert false;
        } catch (Exception e) {
            assert true;
        }
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) ApplicationContextException(cn.taketoday.context.ApplicationContextException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

AutowireCapableBeanFactory (cn.taketoday.beans.factory.config.AutowireCapableBeanFactory)8 Test (org.junit.jupiter.api.Test)5 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)3 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)2 ApplicationContextException (cn.taketoday.context.ApplicationContextException)2 Parameter (java.lang.reflect.Parameter)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 SuppliedMethodInterceptor (cn.taketoday.aop.support.SuppliedMethodInterceptor)1 BeanFactory (cn.taketoday.beans.factory.BeanFactory)1 AnnotatedBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)1 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)1 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)1 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)1 ConfigurationException (cn.taketoday.core.ConfigurationException)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1