Skip to content

Commit a557878

Browse files
committed
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
1 parent df5bab3 commit a557878

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,13 +46,20 @@
4646
* declared value type. In case of a Map, the keys must be declared as
4747
* type String and will be resolved to the corresponding bean names.
4848
*
49-
* <p>Please do consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor}
49+
* <p>Note that actual injection is performed through a
50+
* {@link org.springframework.beans.factory.config.BeanPostProcessor
51+
* BeanPostProcessor} which in turn means that you <em>cannot</em>
52+
* use {@code @Autowired} to inject references into
53+
* {@link org.springframework.beans.factory.config.BeanPostProcessor
54+
* BeanPostProcessor} or {@link BeanFactoryPostProcessor} types. Please
55+
* consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor}
5056
* class (which, by default, checks for the presence of this annotation).
5157
*
5258
* @author Juergen Hoeller
5359
* @author Mark Fisher
5460
* @since 2.5
5561
* @see AutowiredAnnotationBeanPostProcessor
62+
* @see Value
5663
*/
5764
@Retention(RetentionPolicy.RUNTIME)
5865
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})

org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
6363
* that autowires annotated fields, setter methods and arbitrary config methods.
6464
* Such members to be injected are detected through a Java 5 annotation:
65-
* by default, Spring's {@link Autowired} annotation.
65+
* by default, Spring's @{@link Autowired} and @{@link Value} annotations.
6666
*
6767
* <p>Only one constructor (at max) of any given bean class may carry this
6868
* annotation with the 'required' parameter set to <code>true</code>,
@@ -83,21 +83,23 @@
8383
* a special case of such a general config method. Such config methods
8484
* do not have to be public.
8585
*
86-
* <p>Also supports JSR-330's {@link javax.inject.Inject} annotation, if available.
86+
* <p>Also supports JSR-330's {@link javax.inject.Inject @Inject} annotation, if
87+
* available.
8788
*
8889
* <p>Note: A default AutowiredAnnotationBeanPostProcessor will be registered
8990
* by the "context:annotation-config" and "context:component-scan" XML tags.
9091
* Remove or turn off the default annotation configuration there if you intend
9192
* to specify a custom AutowiredAnnotationBeanPostProcessor bean definition.
92-
* <p><b>NOTE:</b> Annotation injection will be performed <i>before</i> XML injection; thus
93-
* the latter configuration will override the former for properties wired through
94-
* both approaches.
95-
*
93+
* <p><b>NOTE:</b> Annotation injection will be performed <i>before</i> XML injection;
94+
* thus the latter configuration will override the former for properties wired through
95+
* both approaches.
96+
*
9697
* @author Juergen Hoeller
9798
* @author Mark Fisher
9899
* @since 2.5
99100
* @see #setAutowiredAnnotationType
100101
* @see Autowired
102+
* @see Value
101103
*/
102104
public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
103105
implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {

org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/Value.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,8 +31,19 @@
3131
* <p>A common use case is to assign default field values using
3232
* "#{systemProperties.myProp}" style expressions.
3333
*
34+
* <p>Note that actual processing of the {@code @Value} annotation is performed
35+
* by a {@link org.springframework.beans.factory.config.BeanPostProcessor
36+
* BeanPostProcessor} which in turn means that you <em>cannot</em> use
37+
* {@code @Value} within
38+
* {@link org.springframework.beans.factory.config.BeanPostProcessor
39+
* BeanPostProcessor} or {@link BeanFactoryPostProcessor} types. Please
40+
* consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor}
41+
* class (which, by default, checks for the presence of this annotation).
42+
*
3443
* @author Juergen Hoeller
3544
* @since 3.0
45+
* @see AutowiredAnnotationBeanPostProcessor
46+
* @see Value
3647
* @see org.springframework.beans.factory.config.BeanExpressionResolver
3748
* @see org.springframework.beans.factory.support.AutowireCandidateResolver#getSuggestedValue
3849
*/

spring-framework-reference/src/beans-annotation-config.xml

+16
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
interfaces that are well-known resolvable dependencies:
278278
<interfacename>BeanFactory</interfacename>,
279279
<interfacename>ApplicationContext</interfacename>,
280+
<interfacename>Environment</interfacename>,
280281
<interfacename>ResourceLoader</interfacename>,
281282
<interfacename>ApplicationEventPublisher</interfacename>, and
282283
<interfacename>MessageSource</interfacename>. These interfaces and their
@@ -295,6 +296,21 @@
295296

296297
<lineannotation>// ...</lineannotation>
297298
}</programlisting>
299+
300+
<note>
301+
<para>
302+
<interfacename>@Autowired</interfacename>,
303+
<interfacename>@Inject</interfacename>,
304+
<interfacename>@Resource</interfacename>, and
305+
<interfacename>@Value</interfacename> annotations are handled by a
306+
Spring <interfacename>BeanPostProcessor</interfacename> implementations
307+
which in turn means that you <emphasis>cannot</emphasis>
308+
apply these annotations within your own
309+
<classname>BeanPostProcessor</classname> or
310+
<classname>BeanFactoryPostProcessor</classname> types (if any). These
311+
types must be 'wired up' explicitly via XML or using a Spring
312+
<interfacename>@Bean</interfacename> method.</para>
313+
</note>
298314
</section>
299315

300316
<section id="beans-autowired-annotation-qualifiers">

0 commit comments

Comments
 (0)