diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index ece2f81c14..a8361f302e 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -135,6 +135,7 @@ import org.springframework.util.StringValueResolver; * both approaches. * * @author Juergen Hoeller + * @author Sam Brannen * @since 2.5 * @see #setAlwaysUseJndiLookup * @see #setResourceFactory @@ -146,40 +147,23 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable { @Nullable - private static Class webServiceRefClass; + private static final Class webServiceRefClass; @Nullable - private static Class ejbRefClass; + private static final Class ejbClass; private static final Set> resourceAnnotationTypes = new LinkedHashSet<>(4); static { - try { - @SuppressWarnings("unchecked") - Class clazz = (Class) - ClassUtils.forName("javax.xml.ws.WebServiceRef", CommonAnnotationBeanPostProcessor.class.getClassLoader()); - webServiceRefClass = clazz; - } - catch (ClassNotFoundException ex) { - webServiceRefClass = null; - } - - try { - @SuppressWarnings("unchecked") - Class clazz = (Class) - ClassUtils.forName("javax.ejb.EJB", CommonAnnotationBeanPostProcessor.class.getClassLoader()); - ejbRefClass = clazz; - } - catch (ClassNotFoundException ex) { - ejbRefClass = null; - } + webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef"); + ejbClass = loadAnnotationType("javax.ejb.EJB"); resourceAnnotationTypes.add(Resource.class); if (webServiceRefClass != null) { resourceAnnotationTypes.add(webServiceRefClass); } - if (ejbRefClass != null) { - resourceAnnotationTypes.add(ejbRefClass); + if (ejbClass != null) { + resourceAnnotationTypes.add(ejbClass); } } @@ -386,7 +370,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean } currElements.add(new WebServiceRefElement(field, field, null)); } - else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) { + else if (ejbClass != null && field.isAnnotationPresent(ejbClass)) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException("@EJB annotation is not supported on static fields"); } @@ -418,7 +402,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); currElements.add(new WebServiceRefElement(method, bridgedMethod, pd)); } - else if (ejbRefClass != null && bridgedMethod.isAnnotationPresent(ejbRefClass)) { + else if (ejbClass != null && bridgedMethod.isAnnotationPresent(ejbClass)) { if (Modifier.isStatic(method.getModifiers())) { throw new IllegalStateException("@EJB annotation is not supported on static methods"); } @@ -562,6 +546,18 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean } + @SuppressWarnings("unchecked") + private static Class loadAnnotationType(String name) { + try { + return (Class) + ClassUtils.forName(name, CommonAnnotationBeanPostProcessor.class.getClassLoader()); + } + catch (ClassNotFoundException ex) { + return null; + } + } + + /** * Class representing generic injection information about an annotated field * or setter method, supporting @Resource and related annotations.