From 26ee0c4842ca83dec213b5422d0af18ba2e3ce6c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 31 Aug 2012 11:55:10 +0200 Subject: [PATCH] aligned with recent changes in CommonAnnotationBeanPostProcessor; backported NPE check Issue: SPR-9176 Issue: SPR-9627 Issue: SPR-9316 --- .../AutowiredAnnotationBeanPostProcessor.java | 25 ++++++++++++------- ...wiredAnnotationBeanPostProcessorTests.java | 7 +++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index ae04086e345..f171863c809 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -405,10 +405,16 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean protected boolean determineRequiredStatus(Annotation annotation) { try { Method method = ReflectionUtils.findMethod(annotation.annotationType(), this.requiredParameterName); + if (method == null) { + // annotations like @Inject and @Value don't have a method (attribute) named "required" + // -> default to required status + return true; + } return (this.requiredParameterValue == (Boolean) ReflectionUtils.invokeMethod(method, annotation)); } catch (Exception ex) { - // required by default + // an exception was thrown during reflective invocation of the required attribute + // -> default to required status return true; } } @@ -419,11 +425,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean private void registerDependentBeans(String beanName, Set autowiredBeanNames) { if (beanName != null) { for (String autowiredBeanName : autowiredBeanNames) { - beanFactory.registerDependentBean(autowiredBeanName, beanName); + if (this.beanFactory.containsBean(autowiredBeanName)) { + this.beanFactory.registerDependentBean(autowiredBeanName, beanName); + } if (logger.isDebugEnabled()) { - logger.debug( - "Autowiring by type from bean name '" + beanName + "' to bean named '" + autowiredBeanName + - "'"); + logger.debug("Autowiring by type from bean name '" + beanName + + "' to bean named '" + autowiredBeanName + "'"); } } } @@ -435,11 +442,11 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean private Object resolvedCachedArgument(String beanName, Object cachedArgument) { if (cachedArgument instanceof DependencyDescriptor) { DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument; - TypeConverter typeConverter = beanFactory.getTypeConverter(); - return beanFactory.resolveDependency(descriptor, beanName, null, typeConverter); + TypeConverter typeConverter = this.beanFactory.getTypeConverter(); + return this.beanFactory.resolveDependency(descriptor, beanName, null, typeConverter); } else if (cachedArgument instanceof RuntimeBeanReference) { - return beanFactory.getBean(((RuntimeBeanReference) cachedArgument).getBeanName()); + return this.beanFactory.getBean(((RuntimeBeanReference) cachedArgument).getBeanName()); } else { return cachedArgument; diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index b2967a33aa7..80e8bc7725e 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -118,6 +118,11 @@ public final class AutowiredAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean4()); assertSame(ntb, bean.getNestedTestBean()); assertSame(bf, bean.getBeanFactory()); + + String[] depBeans = bf.getDependenciesForBean("annotatedBean"); + assertEquals(2, depBeans.length); + assertEquals("testBean", depBeans[0]); + assertEquals("nestedTestBean", depBeans[1]); } @Test