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

XML config ignores autowire="no" #33919

Open
grahamcox-oclc opened this issue Nov 19, 2024 · 0 comments
Open

XML config ignores autowire="no" #33919

grahamcox-oclc opened this issue Nov 19, 2024 · 0 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@grahamcox-oclc
Copy link

We've got an application that uses a lot of XML wiring - since it dates back to before Java config was a thing.

We've recently discovered that Spring is autowiring in beans when we didn't expect it to - we'd got a typo in a constructor argument name and instead of an error things were still running.

I've since reproduced this in a trivial application - which is attached.
autowire-demo.zip

In this, we've got:

  • A ServiceB class
  • A ServiceA class that requires an instance of ServiceB
  • A controller class that requires an instance of ServiceA. (This was simply to be able to test the wiring)
  • A spring.xml context file for constructing ServiceA and ServiceB.

However, you'll notice that in spring.xml we've got this:

    <bean id="serviceB" class="com.example.autowire_demo.ServiceB" />

    <bean id="serviceA" class="com.example.autowire_demo.ServiceA" autowire="no">
    </bean>

So the ServiceA instance has explicitly got autowire="no" on it, but is missing the constructor-arg that it needs.

If you run this then you will get a working service. Spring is autowiring serviceB into serviceA even though it shouldn't.

In a similar manner - which I assume is related but I don't know for certain - we've noticed that if the constructor argument is a List (or probably other collection types) and it's missing from the XML then Spring autowires in an empty collection instead of erroring. This is actually worse since at least with the above it's probably the case that there's only one candidate and so the right thing will happen by luck. With the list case, you're definitely getting the wrong behaviour.

Unfortunately this now means that we might have some missing/broken wiring and Spring isn't alerting us to the issue.

Cheers

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 19, 2024
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 20, 2024
@sbrannen sbrannen changed the title autowire="no" is ignored in XML config XML config ignores autowire="no" Nov 20, 2024
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: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

3 participants