use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.
the class CommonAnnotationBeanPostProcessorTests method testExtendedResourceInjectionWithOverriding.
@Test
public void testExtendedResourceInjectionWithOverriding() {
StandardBeanFactory bf = new StandardBeanFactory();
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerDependency(BeanFactory.class, bf);
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Properties props = new Properties();
props.setProperty("tb", "testBean3");
ppc.setProperties(props);
ppc.postProcessBeanFactory(bf);
BeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
TestBean tb5 = new TestBean();
annotatedBd.getPropertyValues().add("testBean2", tb5);
bf.registerBeanDefinition("annotatedBean", annotatedBd);
bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
TestBean tb = new TestBean();
bf.registerSingleton("testBean", tb);
TestBean tb2 = new TestBean();
bf.registerSingleton("testBean2", tb2);
TestBean tb3 = new TestBean();
bf.registerSingleton("testBean3", tb3);
TestBean tb4 = new TestBean();
bf.registerSingleton("testBean4", tb4);
NestedTestBean tb6 = new NestedTestBean();
bf.registerSingleton("xy", tb6);
ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
assertThat(bean.initCalled).isTrue();
assertThat(bean.init2Called).isTrue();
assertThat(bean.getTestBean()).isSameAs(tb);
assertThat(bean.getTestBean2()).isSameAs(tb5);
assertThat(bean.getTestBean3()).isSameAs(tb4);
assertThat(bean.getTestBean4()).isSameAs(tb3);
assertThat(bean.testBean5).isSameAs(tb6);
assertThat(bean.testBean6).isSameAs(tb6);
assertThat(bean.beanFactory).isSameAs(bf);
try {
bf.getBean("annotatedBean2");
} catch (BeanCreationException ex) {
boolean condition = ex.getRootCause() instanceof NoSuchBeanDefinitionException;
assertThat(condition).isTrue();
NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
assertThat(innerEx.getBeanName()).isEqualTo("testBean9");
}
bf.destroySingletons();
assertThat(bean.destroyCalled).isTrue();
assertThat(bean.destroy2Called).isTrue();
}
use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.
the class ConstructorResolver method resolveAutowiredArgument.
/**
* Template method for resolving the specified argument which is supposed to be autowired.
*/
@Nullable
private Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter, boolean fallback) {
Class<?> paramType = param.getParameterType();
if (InjectionPoint.class.isAssignableFrom(paramType)) {
InjectionPoint injectionPoint = currentInjectionPoint.get();
if (injectionPoint == null) {
throw new IllegalStateException("No current InjectionPoint available for " + param);
}
return injectionPoint;
}
try {
DependencyResolvingContext context = new DependencyResolvingContext(param.getExecutable(), beanFactory, beanName);
context.setTypeConverter(typeConverter);
context.setDependentBeans(autowiredBeanNames);
return injector.resolveValue(new DependencyDescriptor(param, true), context);
} catch (NoUniqueBeanDefinitionException ex) {
throw ex;
} catch (NoSuchBeanDefinitionException ex) {
if (fallback) {
// for e.g. a vararg or a non-null List/Set/Map parameter.
if (paramType.isArray()) {
return Array.newInstance(paramType.getComponentType(), 0);
} else if (CollectionUtils.isApproximableCollectionType(paramType)) {
return CollectionUtils.createCollection(paramType, 0);
} else if (CollectionUtils.isApproximableMapType(paramType)) {
return CollectionUtils.createMap(paramType, 0);
}
}
throw ex;
}
}
use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.
the class NoUniqueBeanDefinitionFailureAnalyzer method buildMessage.
private void buildMessage(StringBuilder message, String beanName) {
try {
BeanDefinition definition = BeanFactoryUtils.requiredDefinition(beanFactory, beanName);
message.append(getDefinitionDescription(beanName, definition));
} catch (NoSuchBeanDefinitionException ex) {
message.append(String.format("\t- %s: a programmatically registered singleton", beanName));
}
}
use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.
the class QualifierAnnotationAutowireCandidateResolver method checkQualifier.
/**
* Match the given qualifier annotation against the candidate bean definition.
*/
protected boolean checkQualifier(BeanDefinition definition, Annotation annotation, SimpleTypeConverter typeConverter) {
Class<? extends Annotation> type = annotation.annotationType();
RootBeanDefinition bd = (RootBeanDefinition) definition;
AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
if (qualifier == null) {
qualifier = bd.getQualifier(ClassUtils.getShortName(type));
}
if (qualifier == null) {
// First, check annotation on qualified element, if any
Annotation targetAnnotation = getQualifiedElementAnnotation(bd, type);
// Then, check annotation on factory method, if applicable
if (targetAnnotation == null) {
targetAnnotation = getFactoryMethodAnnotation(bd, type);
}
if (targetAnnotation == null) {
RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
if (dbd != null) {
targetAnnotation = getFactoryMethodAnnotation(dbd, type);
}
}
if (targetAnnotation == null) {
// Look for matching annotation on the target class
if (getBeanFactory() != null) {
try {
Class<?> beanType = getBeanFactory().getType(definition.getBeanName());
if (beanType != null) {
targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
}
} catch (NoSuchBeanDefinitionException ex) {
// Not the usual case - simply forget about the type check...
}
}
if (targetAnnotation == null && bd.hasBeanClass()) {
targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
}
}
if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
return true;
}
}
Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
if (attributes.isEmpty() && qualifier == null) {
// If no attributes, the qualifier must be present
return false;
}
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String attributeName = entry.getKey();
Object expectedValue = entry.getValue();
Object actualValue = null;
// Check qualifier first
if (qualifier != null) {
actualValue = qualifier.getAttribute(attributeName);
}
if (actualValue == null) {
// Fall back on bean definition attribute
actualValue = bd.getAttribute(attributeName);
}
if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) && expectedValue instanceof String && definition.matchesName((String) expectedValue)) {
// Fall back on bean name (or alias) match
continue;
}
if (actualValue == null && qualifier != null) {
// Fall back on default, but only if the qualifier is present
actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
}
if (actualValue != null) {
actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
}
if (!expectedValue.equals(actualValue)) {
return false;
}
}
return true;
}
use of cn.taketoday.beans.factory.NoSuchBeanDefinitionException in project today-framework by TAKETODAY.
the class ContextAnnotationAutowireCandidateResolver method buildLazyResolutionProxy.
protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, @Nullable final String beanName) {
BeanFactory beanFactory = getBeanFactory();
Assert.state(beanFactory instanceof StandardBeanFactory, "BeanFactory needs to be a StandardBeanFactory");
final StandardBeanFactory dlbf = (StandardBeanFactory) beanFactory;
TargetSource ts = new TargetSource() {
@Override
public Class<?> getTargetClass() {
return descriptor.getDependencyType();
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() {
Set<String> autowiredBeanNames = beanName != null ? new LinkedHashSet<>(1) : null;
Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
if (target == null) {
Class<?> type = getTargetClass();
if (Map.class == type) {
return Collections.emptyMap();
} else if (List.class == type) {
return Collections.emptyList();
} else if (Set.class == type || Collection.class == type) {
return Collections.emptySet();
}
throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(), "Optional dependency not present for lazy injection point");
}
if (autowiredBeanNames != null) {
for (String autowiredBeanName : autowiredBeanNames) {
if (dlbf.containsBean(autowiredBeanName)) {
dlbf.registerDependentBean(autowiredBeanName, beanName);
}
}
}
return target;
}
@Override
public void releaseTarget(Object target) {
}
};
ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
Class<?> dependencyType = descriptor.getDependencyType();
if (dependencyType.isInterface()) {
pf.addInterface(dependencyType);
}
return pf.getProxy(dlbf.getBeanClassLoader());
}
Aggregations