7

I want to create two beans of the same class through xml config file. The class has an annotated setter to be filled by spring. In one of the bean definitions I also provide value manually to override one from annotation. But when I do this, spring no longer handles annotation wiring.

Here is a minimal code to demonstrate this effect, used @Value for simplicity but it's the same with @Autowired:

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

public class AutowireTest {
  public String testField;

  @PostConstruct
  public void init() {
    if (testField == null)
      throw new RuntimeException("FAIL");
  }

  @Value("default")
  public void setTestField(String testField) {
    this.testField = testField;
  }
}

and spring config:

<bean id="au_test1" class="AutowireTest">
  <property name="testField" value="manual"/>
</bean>

<bean id="au_test2" class="AutowireTest"/>

If I remove <property name="testField" value="manual"/>, both beans receive "default". If it's there the second bean throws an exception. I had a look at spring code and AutowiredAnnotationBeanPostProcessor is using injectionMetadataCache where classes are keys for injection metadata meaning setting particular property for one bean disables autowiring for the other instances.

Any idea why it's this way? How can I achieve a similar effect working not necessarily for string values but also for object beans?

EDIT: I want to have a property where there are multiple matching candidates. One is marked as primary. Unless I specify a candidate manually through xml, I would like to have the primary one wired. My original approach was to use @Autowired for this property but since this does not work I'm looking for an alternative. For some reason I don't want to use bean inheritance.

EDIT2: If I swap these two bean definitions, the problem does not occur. The property is autowired fine until manual override is detected for the first time. This means this is not an intented feature since it can lead to weird and hard to detect bugs in some projects with dependencies not being wired as expected.

2
  • 1
    I think the implementation is this way in order to provide better performances.
    – ndeverge
    Feb 28, 2012 at 17:25
  • I have submitted it to spring jira with a minimal project demonstrating the issue: jira.springsource.org/browse/SPR-9183
    – mrembisz
    Mar 7, 2012 at 9:08

2 Answers 2

1

If you take a look at the javaDocs for the AutowiredAnnotationBeanPostProcessor, there is a note stating:

"Annotation injection will be performed before XML injection; thus the latter configuration will override the former for properties wired through both approaches."

This means that setting your beans property in xml with and with out a property value for "testField" makes a two new beans one where testField is set and one where it is null.

Its a one or the other thing. you can either use autowiring for a class or set it up manually in xml but cant do both. Im guessing that when your removing the property spring is just setting the same autowired bean to both names.

Once the beans of the same class are set up manually you can inject them into other beans using the @Qualifier("au_test1") annotation

2
  • Thank you for your answer. I did read javadoc but I can't see how it explains what I observe. Also I have updated my question to explain what I want to achieve.
    – mrembisz
    Feb 28, 2012 at 22:47
  • I made another update to my question which points out this behavior is affected by order in which beans are declared. I guess that clearly means this is a bug rather than an intended feature.
    – mrembisz
    Feb 29, 2012 at 8:27
0

In my opinion it is a bug in spring which I have submitted through jira. To work around this, annotation must be moved from setter to field. Setter must stay to allow manual overriding of the property. Possible setter logic (I know this should be avoided) should be moved to @PostConstruct. Adapted original code below:

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

public class AutowireTest {
  @Value("default")
  public String testField;

  @PostConstruct
  public void init() {
    if (testField == null)
      throw new RuntimeException("FAIL");
  }

  public void setTestField(String testField) {
    this.testField = testField;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.