-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Allow @Bean methods to override definitions in XML [SPR-7028] #11690
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
Comments
Chris Beams commented Scheduling for 3.1 timeline. This is certainly an important issue, and will require a bit of thought as to exactly how we implement it. At this point, something user-configurable, probably on It could be argued that overriding semantics should be exposed on the Even this approach has edge case issues, however. It could be that XML exposes <bean/> definitions b1 and b2, and also includes |
Dave Syer commented I don't think we need to make it complicated. What exactly are the use cases driving the current behaviour anyway? The semantics are well defined for bean definition readers and bean factories: bean definitions parsed later override those parsed earlier. Let's try and keep that guarantee and work out how to make the ordering of resources (class, xml, etc) easy to reason about. E.g. it seems intuitive to me that an |
Chris Beams commented test from mylyn. disregard. |
Neale Upstone commented +1 on Dave's suggestion here. I don't exactly love the default behaviour of overriding (personally thinking that nested |
Chris Beams commented Okay, I've given this some thought. Take a look and please provide feedback. If we can keep the scope simple, this can probably still make it into 3.1 M2. Dave's "case 1" above deals with a situation like the following:
The question is who should 'win' -- the The answer could be that the The answer could be that the In my mind, there's not a clear answer to whether the
The above indicates that "beans imported from the some.xml resource should override The default for the Implementation feasibility for this attribute has not been evaluated. I mainly want to see if the approach would meet the needs discussed here. Dave's "Case 2" goes something like the following:
In this case, I agree that the current behavior doesn't make sense. It's pretty clear that the
Now, the user's intent might be to override foo in XML because it comes 'after' the I propose the following: Just as above, where Thoughts? |
Dave Syer commented I could see some value in your proposal for I see what you are saying but I must admit I don't like the proposal for Case 2. I don't like anything that says "XML always wins" or "Java always wins", and I don't like anything that requires me to modify the context loader in Java (that's like saying to a user he should extend GenericApplicationContext). The existing behaviour of XML where later bean definitions override earlier ones makes sense, so what we should focus on is retaining those semantics and defining carefully the meaning of "later". Including a bean definition of an |
Chris Beams commented Slating this issue to be addressed in 3.2, when we plan to address all 'bean-visibility-and-overriding'-labeled issues (see tracking issue #12839). This will also allow more time for feedback post 3.1 GA to see what actual usage patterns look like. |
Sam Brannen commented
I think that is completely logical and expected by most users. The fact that it is analogous to the semantics of XML configuration with imports strengthens the argument of going with this as the default semantics for
I disagree. See above. ;)
Instead of (or perhaps in addition to) introducing an Thoughts? Sam |
Dave Syer commented
That works for me and I think it is intuitive for users. |
thomas menzel commented this just cost me 5hrs of my life to figure out why my overriding and guys: this issue is 5 yrs old! |
Conor Gallagher commented I highly suggest keeping the current behaviour as default, the blast-radius would be far larger than previous comments suggest. For example, our product has an OOTB wiring via a combination of If the precedence were to be changed in a future Spring release without us noticing, then the behaviour of our customer deployments would quietly change. |
Stéphane Nicoll commented Conor Gallagher you could fix that by having a deterministic order for the product's configuration, making sure that those customizations happens last. I'd argue that you should do this independently of the current mechanism. |
Conor Gallagher commented I'm just trying to call out that this would be a massive breaking change, the impact of which could be devastating. Below is simply not true:
Imagine a scenario where a developer revs the version of Spring without reading the release notes throughly! :) Everything works as before and all their tests pass, but one subtle thing has changed in production. Their setup is something along the lines of @Configuration
@ImportResource("classpath:spring/customer-config.xml")
public class BaseConfig {
@Bean(name = "paymentProcessor")
public PaymentProcessor getPaymentProcessor() {
return new DummyPaymentProcessor();
}
} Their individual customers define the appropriate payment processors via xml as follows: <beans >
<bean id="paymentProcessor" class="com.visa.VisaPaymentProcessor" />
</beans> In all their test environments the behaviour of the application is unchanged, but in production the Visa paymentProcessor they defined is no longer getting used. All payments are now being processed by a dummy payment processor! Obviously this is a contrived example, and I'm not advocating this setup. But please keep this sort of thing in mind before you change the default behaviour. |
Stéphane Nicoll commented Conor Gallagher with such a reasoning, we wouldn't be able to evolve anything at all, really. I am not sure that "not reading the release notes" is an argument either. 5.0 is out now and this issue hasn't been assigned so we're not going to change that any time soon. I already gave you an escape path that you can apply right now. |
Juergen Hoeller commented The main reason why we have not changed this particular arrangement for many years is exactly the backwards compatibility consideration. We indeed reserve the right to change defaults in some cases... but arguably not in this one. In particular since overriding between XML and Java config arrangements is not recommendable to begin with; it's complicated enough if you override within an XML bean definition arrangement. In short: The default behavior is not going to change here. We'd only do that if there was another first-class scenario that we couldn't make work otherwise. |
Juergen Hoeller commented I'm marking this as "Won't Fix" to send the right message: namely that we have no intentions of messing with it at this late point. We recommend against bean overriding to begin with, preferring profiles or custom conditions instead. If you have to override, don't override instance-based XML definitions with class-based Finally, for 5.0, we're actually considering to disallow bean definition overriding to begin with (#15434), having to opt in instead of opt out. This would mean that you'd get an exception for any kind of override attempt. |
Dave Syer opened SPR-7028 and commented
There are two ways I might want to do this, and both fail.
@ImportResource
in an@Configuration
to load an XML file, then override a bean using@Bean
@Configuration
, and the latter provides a bean with the same nameNeither works.
Case 1 fails because
ConfigurationClassBeanDefinitionReader
always loads XML imports after the@Bean
definitions. This seems like the wrong order, so it would be good to understand why it is implemented that way (I imported before I defined the@Bean
, so I expect the latter to win).This code in
ConfigurationClassBeanDefinitionReader
prevents the override in case 2:Affects: 3.0.1
Issue Links:
@Configuration
classes to override those defined in XML8 votes, 12 watchers
The text was updated successfully, but these errors were encountered: