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;
}
}
}
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;
}
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();
}
}
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);
}
}
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;
}
}
}
Aggregations