You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Annotation driven injection on public methods specified in a non-public base classes does not work, because Java generates bridge methods for the subclass which are not ignored by Spring (as they should IMHO).
Example:
/* not public! */ class Base {
@Resource
public void setResource(Injectable injectable) {
...
}
}
@Component
public class Target extends Base {
}
Same applies to @Autowired.
The attached maven project demonstrates this.
Workarounds: Use field injection, re-declare the method(s) in the subclass or make the base class public.
Thanks for the test project. I'm reproducing this locally and we'll definitely patch things up here. Scheduling for inclusion in 3.0.6 as well as 3.1 RC1 as the fix will likely be worth backporting.
Author: Chris Beams <cbeams@vmware.com>
Date: Tue Jun 21 22:23:54 2011 +0800
Fix bridge method detection for class hierarchies
Prior to this change, @Autowired injection against an instance of a
subclass having different visibility than its superclass would fail
if the @Autowired method is declared only in the superclass. This is due
to an apparent change in the rules around bridge method generation
between Java 5 and Java 6, and possibly even varying across compiler
vendors.
Now, BridgeMethodResolver is used consistently when detecting
@Autowired, @Inject and @Resource metadata to bypass these bridge
methods if they exist.
Issue: SPR-7900
Andreas Benneke opened SPR-7900 and commented
Annotation driven injection on public methods specified in a non-public base classes does not work, because Java generates bridge methods for the subclass which are not ignored by Spring (as they should IMHO).
Example:
Same applies to
@Autowired
.The attached maven project demonstrates this.
Workarounds: Use field injection, re-declare the method(s) in the subclass or make the base class public.
Affects: 3.0.5
Attachments:
Issue Links:
@Resource
injection broken under spring-framework > 4.1.0The text was updated successfully, but these errors were encountered: