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
This commit is contained in:
parent
df5bab3454
commit
a557878a6f
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* 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.
|
* type String and will be resolved to the corresponding bean names.
|
||||||
*
|
*
|
||||||
* <p>Please do consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor}
|
* <p>Note that actual injection is performed through a
|
||||||
|
* {@link org.springframework.beans.factory.config.BeanPostProcessor
|
||||||
|
* BeanPostProcessor} which in turn means that you <em>cannot</em>
|
||||||
|
* 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).
|
* class (which, by default, checks for the presence of this annotation).
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see AutowiredAnnotationBeanPostProcessor
|
* @see AutowiredAnnotationBeanPostProcessor
|
||||||
|
* @see Value
|
||||||
*/
|
*/
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
|
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ import org.springframework.util.ReflectionUtils;
|
||||||
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
|
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
|
||||||
* that autowires annotated fields, setter methods and arbitrary config methods.
|
* that autowires annotated fields, setter methods and arbitrary config methods.
|
||||||
* Such members to be injected are detected through a Java 5 annotation:
|
* 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.
|
||||||
*
|
*
|
||||||
* <p>Only one constructor (at max) of any given bean class may carry this
|
* <p>Only one constructor (at max) of any given bean class may carry this
|
||||||
* annotation with the 'required' parameter set to <code>true</code>,
|
* annotation with the 'required' parameter set to <code>true</code>,
|
||||||
|
|
@ -83,14 +83,15 @@ import org.springframework.util.ReflectionUtils;
|
||||||
* a special case of such a general config method. Such config methods
|
* a special case of such a general config method. Such config methods
|
||||||
* do not have to be public.
|
* do not have to be public.
|
||||||
*
|
*
|
||||||
* <p>Also supports JSR-330's {@link javax.inject.Inject} annotation, if available.
|
* <p>Also supports JSR-330's {@link javax.inject.Inject @Inject} annotation, if
|
||||||
|
* available.
|
||||||
*
|
*
|
||||||
* <p>Note: A default AutowiredAnnotationBeanPostProcessor will be registered
|
* <p>Note: A default AutowiredAnnotationBeanPostProcessor will be registered
|
||||||
* by the "context:annotation-config" and "context:component-scan" XML tags.
|
* by the "context:annotation-config" and "context:component-scan" XML tags.
|
||||||
* Remove or turn off the default annotation configuration there if you intend
|
* Remove or turn off the default annotation configuration there if you intend
|
||||||
* to specify a custom AutowiredAnnotationBeanPostProcessor bean definition.
|
* to specify a custom AutowiredAnnotationBeanPostProcessor bean definition.
|
||||||
* <p><b>NOTE:</b> Annotation injection will be performed <i>before</i> XML injection; thus
|
* <p><b>NOTE:</b> Annotation injection will be performed <i>before</i> XML injection;
|
||||||
* the latter configuration will override the former for properties wired through
|
* thus the latter configuration will override the former for properties wired through
|
||||||
* both approaches.
|
* both approaches.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
|
@ -98,6 +99,7 @@ import org.springframework.util.ReflectionUtils;
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see #setAutowiredAnnotationType
|
* @see #setAutowiredAnnotationType
|
||||||
* @see Autowired
|
* @see Autowired
|
||||||
|
* @see Value
|
||||||
*/
|
*/
|
||||||
public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
|
public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
|
||||||
implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
|
implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -31,8 +31,19 @@ import java.lang.annotation.Target;
|
||||||
* <p>A common use case is to assign default field values using
|
* <p>A common use case is to assign default field values using
|
||||||
* "#{systemProperties.myProp}" style expressions.
|
* "#{systemProperties.myProp}" style expressions.
|
||||||
*
|
*
|
||||||
|
* <p>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 <em>cannot</em> 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
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
|
* @see AutowiredAnnotationBeanPostProcessor
|
||||||
|
* @see Value
|
||||||
* @see org.springframework.beans.factory.config.BeanExpressionResolver
|
* @see org.springframework.beans.factory.config.BeanExpressionResolver
|
||||||
* @see org.springframework.beans.factory.support.AutowireCandidateResolver#getSuggestedValue
|
* @see org.springframework.beans.factory.support.AutowireCandidateResolver#getSuggestedValue
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -277,6 +277,7 @@
|
||||||
interfaces that are well-known resolvable dependencies:
|
interfaces that are well-known resolvable dependencies:
|
||||||
<interfacename>BeanFactory</interfacename>,
|
<interfacename>BeanFactory</interfacename>,
|
||||||
<interfacename>ApplicationContext</interfacename>,
|
<interfacename>ApplicationContext</interfacename>,
|
||||||
|
<interfacename>Environment</interfacename>,
|
||||||
<interfacename>ResourceLoader</interfacename>,
|
<interfacename>ResourceLoader</interfacename>,
|
||||||
<interfacename>ApplicationEventPublisher</interfacename>, and
|
<interfacename>ApplicationEventPublisher</interfacename>, and
|
||||||
<interfacename>MessageSource</interfacename>. These interfaces and their
|
<interfacename>MessageSource</interfacename>. These interfaces and their
|
||||||
|
|
@ -295,6 +296,21 @@
|
||||||
|
|
||||||
<lineannotation>// ...</lineannotation>
|
<lineannotation>// ...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
|
<note>
|
||||||
|
<para>
|
||||||
|
<interfacename>@Autowired</interfacename>,
|
||||||
|
<interfacename>@Inject</interfacename>,
|
||||||
|
<interfacename>@Resource</interfacename>, and
|
||||||
|
<interfacename>@Value</interfacename> annotations are handled by a
|
||||||
|
Spring <interfacename>BeanPostProcessor</interfacename> implementations
|
||||||
|
which in turn means that you <emphasis>cannot</emphasis>
|
||||||
|
apply these annotations within your own
|
||||||
|
<classname>BeanPostProcessor</classname> or
|
||||||
|
<classname>BeanFactoryPostProcessor</classname> types (if any). These
|
||||||
|
types must be 'wired up' explicitly via XML or using a Spring
|
||||||
|
<interfacename>@Bean</interfacename> method.</para>
|
||||||
|
</note>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section id="beans-autowired-annotation-qualifiers">
|
<section id="beans-autowired-annotation-qualifiers">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue