Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Autowired List<ToAutoWire> into Configuration that defines @Bean ToAutoWire fails [SPR-12646] #17247

Closed
spring-projects-issues opened this issue Jan 19, 2015 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 19, 2015

Rob Winch opened SPR-12646 and commented

Starting with Spring 4.0.7 (4.0.6 works) the following setup will fail because only the ToAutoWire("a") will be Autowired :

public class ToAutoWire {
    private final String name;

    public ToAutoWire(String name) {
        super();
        this.name = name;
    }
}

@Configuration
public class ConfigA {

    @Bean
    public ToAutoWire a() {
        return new ToAutoWire("a");
    }
}

@Configuration
public class ConfigB {
    List<ToAutoWire> values;

    @Bean
    public ToAutoWire b() {
        return new ToAutoWire("b");
    }

    @Autowired
    public void setValues(List<ToAutoWire> values) {
        this.values = values;
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConfigA.class, ConfigB.class})
public class AutowireListTests {
    @Autowired
    ConfigB b;

    @Test
    public void run() {
        assertEquals(2,b.values.size());
    }
}

A few notes:


Affects: 4.0.7, 4.0.8, 4.0.9, 4.1 GA, 4.1.1, 4.1.2, 4.1.3, 4.1.4

Reference URL: https://github.com/rwinch/spring-framework-issues/tree/SPR-12646

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 20, 2015

Juergen Hoeller commented

Rob, I'm afraid this is intentional: There is an inherent lifecycle problem in such a setup, with the b() call having to come in before the configuration class has been fully initialized. Due to the issues pointed out in #16634, we explicitly exclude such beans from injection now.

As a way out, consider declaring the b() method as static, which indicates that no instance of the configuration class is needed at that point. Alternatively, add the b() declared bean manually within your configuration class implementation, if you know that this can be reliably done at that point.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Rob Winch commented

Juergen Hoeller Thank you for your response and the references to the related issues. I find it very surprising that this is intentional for a few reasons.

  • This use to work before
  • Removing ConfigA from my example above causes the ToAutoWire("b") to be autowired. Why does ToAutoWire("b") get autowired when it is the only Bean but not when there are multiple?
  • If this is not intended to work, I feel like this should result in an error rather than silently be dropped.

Given #1 will not be addressed, "Do you think it makes sense to address #2 and #3?"

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 20, 2015

Juergen Hoeller commented

The reason for #2 is Spring 4.0's fallback matching algorithm: Analogous to fallback matches for generic types vs raw types, instead of failing without a result, we'll do a best-effort pass and rather take an 'imperfect' match over none at all. This wasn't really designed for our present case but also applies to our scenario since we're not explicitly preventing it.

So effectively, matches to other beans override self-references back to factory methods on the declaring bean, with no side effects triggered by attempts to resolve the self-references in such a case (which was the goal of #16634, still very much worth having). Just if it turns out that there are no proper matches to other beans, a self-reference is being considered as a fallback. I'm not a great fan of that end result but that's what it is right now. We could consider disallowing the fallback match altogether.

As for raising an error in such a case, we're not doing that for regular self-references either: The autowire candidate algorithm explicitly skips references back to the declaring bean even if it does match the requested type, silently assuming that such self-references aren't intended. This dates back to Spring 2.5 even, IIRC. Now we're also skipping references back to factory methods on the declaring bean, through the same algorithm, and therefore silently proceed in such a case as well.

All of that said, self-references weren't intended to work in the first place and shouldn't be relied on within our own configuration class arrangements. The simplest way out is to change the affected factory methods to static, explicitly declaring that they don't rely on instance state of the containing bean. However, preferably, a configuration class should just ask for injection of external matches and programmatically add defaults if necessary, or have the default instances declared on a separate configuration class that it imports.

For your SEC-2822 purposes, does any of those alternatives work?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Rob Winch commented

Juergen Hoeller Thank you again for your follow up.

In general, I find this behavior very unintuitive and think Spring should try to be more consistent or report an error (preferred).

For your SEC-2822 purposes, does any of those alternatives work?

Yes. I was able to use a static method to declare the Bean and everything is working now. Thank you for pointing me in this direction.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants