From a557878a6f79e1d595c1e6e05faf7cbd78718b58 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Fri, 13 May 2011 03:41:29 +0000 Subject: [PATCH] Document @Autowired and @Value limitations @Autowired, @Value and other annotations cannot be applied within Spring Bean(Factory)PostProcessor types, because they themselves are processed using BeanPostProcessors. Javadoc and reference docs have been updated to reflect. Issue: SPR-4935, SPR-8213 --- .../beans/factory/annotation/Autowired.java | 11 +++++++++-- .../AutowiredAnnotationBeanPostProcessor.java | 14 ++++++++------ .../beans/factory/annotation/Value.java | 13 ++++++++++++- .../src/beans-annotation-config.xml | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java index c3265082537..7bfcce75081 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2011 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. @@ -46,13 +46,20 @@ import java.lang.annotation.Target; * declared value type. In case of a Map, the keys must be declared as * type String and will be resolved to the corresponding bean names. * - *

Please do consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor} + *

Note that actual injection is performed through a + * {@link org.springframework.beans.factory.config.BeanPostProcessor + * BeanPostProcessor} which in turn means that you cannot + * use {@code @Autowired} to inject references into + * {@link org.springframework.beans.factory.config.BeanPostProcessor + * BeanPostProcessor} or {@link BeanFactoryPostProcessor} types. Please + * consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor} * class (which, by default, checks for the presence of this annotation). * * @author Juergen Hoeller * @author Mark Fisher * @since 2.5 * @see AutowiredAnnotationBeanPostProcessor + * @see Value */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD}) 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 81690732fa8..72232c249d3 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 @@ -62,7 +62,7 @@ import org.springframework.util.ReflectionUtils; * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation * that autowires annotated fields, setter methods and arbitrary config methods. * Such members to be injected are detected through a Java 5 annotation: - * by default, Spring's {@link Autowired} annotation. + * by default, Spring's @{@link Autowired} and @{@link Value} annotations. * *

Only one constructor (at max) of any given bean class may carry this * annotation with the 'required' parameter set to true, @@ -83,21 +83,23 @@ import org.springframework.util.ReflectionUtils; * a special case of such a general config method. Such config methods * do not have to be public. * - *

Also supports JSR-330's {@link javax.inject.Inject} annotation, if available. + *

Also supports JSR-330's {@link javax.inject.Inject @Inject} annotation, if + * available. * *

Note: A default AutowiredAnnotationBeanPostProcessor will be registered * by the "context:annotation-config" and "context:component-scan" XML tags. * Remove or turn off the default annotation configuration there if you intend * to specify a custom AutowiredAnnotationBeanPostProcessor bean definition. - *

NOTE: Annotation injection will be performed before XML injection; thus - * the latter configuration will override the former for properties wired through - * both approaches. - * + *

NOTE: Annotation injection will be performed before XML injection; + * thus the latter configuration will override the former for properties wired through + * both approaches. + * * @author Juergen Hoeller * @author Mark Fisher * @since 2.5 * @see #setAutowiredAnnotationType * @see Autowired + * @see Value */ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware { diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Value.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Value.java index 40df0991d71..69bed418a3d 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Value.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Value.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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. @@ -31,8 +31,19 @@ import java.lang.annotation.Target; *

A common use case is to assign default field values using * "#{systemProperties.myProp}" style expressions. * + *

Note that actual processing of the {@code @Value} annotation is performed + * by a {@link org.springframework.beans.factory.config.BeanPostProcessor + * BeanPostProcessor} which in turn means that you cannot use + * {@code @Value} within + * {@link org.springframework.beans.factory.config.BeanPostProcessor + * BeanPostProcessor} or {@link BeanFactoryPostProcessor} types. Please + * consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor} + * class (which, by default, checks for the presence of this annotation). + * * @author Juergen Hoeller * @since 3.0 + * @see AutowiredAnnotationBeanPostProcessor + * @see Value * @see org.springframework.beans.factory.config.BeanExpressionResolver * @see org.springframework.beans.factory.support.AutowireCandidateResolver#getSuggestedValue */ diff --git a/spring-framework-reference/src/beans-annotation-config.xml b/spring-framework-reference/src/beans-annotation-config.xml index 4a19f60078d..6f4ececae7d 100644 --- a/spring-framework-reference/src/beans-annotation-config.xml +++ b/spring-framework-reference/src/beans-annotation-config.xml @@ -277,6 +277,7 @@ interfaces that are well-known resolvable dependencies: BeanFactory, ApplicationContext, + Environment, ResourceLoader, ApplicationEventPublisher, and MessageSource. These interfaces and their @@ -295,6 +296,21 @@ // ... } + + + + @Autowired, + @Inject, + @Resource, and + @Value annotations are handled by a + Spring BeanPostProcessor implementations + which in turn means that you cannot + apply these annotations within your own + BeanPostProcessor or + BeanFactoryPostProcessor types (if any). These + types must be 'wired up' explicitly via XML or using a Spring + @Bean method. +