-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
Loïc Frering opened SPR-8660 and commented
Due to changeset 4600 in AutowiredAnnotationBeanProcessor, autowired annotations are found on overrided parameterized methods where there are no @Autowired
annotations!
Exemple :
public abstract class GenericServiceImpl<T, ID extends Serializable, D extends GenericDao<T, ID>> implements GenericService<T, ID> {
protected D dao;
public void setDao(D resourceDao) {
this.dao = resourceDao;
}
// ...
}
@Service("userService")
public class UserServiceImpl extends GenericServiceImpl<User, Integer, UserDao> implements UserService {
@Inject
@Named("userDao")
@Override
public void setDao(UserDao userDao) {
super.setDao(userDao);
}
// ...
}
When processing the userService bean, the findAutowiredAnnotation method in AutowiredAnnotationBeanPostProcessor finds not only the @Autowired
annotation for the setDao method defined in UserServiceImpl but also for the one defined in the parent class. This because the BridgeMethodResolver.findBridgedMethod detects the child method as the bridged method of the parent one:
private Annotation findAutowiredAnnotation(AccessibleObject ao) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
if (ao instanceof Method) {
ao = BridgeMethodResolver.findBridgedMethod((Method) ao);
}
Annotation annotation = ao.getAnnotation(type);
if (annotation != null) {
return annotation;
}
}
return null;
}
This leads to an autowire by type for the parameterized parent setDao method which fail because there are many implementations available and @Named
is not detected in the reflected method, the right one!
I think that there is a fix to do in BridgeMethodResolver to be more specific and avoid wrong bridged method detections.
By the way I think that this issue might affect SpringData which uses a lot Java generics...
Affects: 3.0.6, 3.1 M2
Issue Links:
- Annotation based injection into non public base classes does not work [SPR-7900] #12555 Annotation based injection into non public base classes does not work
- Java 8 bridge method handling can lead to false positive detection of autowired annotations [SPR-12187] #16801 Java 8 bridge method handling can lead to false positive detection of autowired annotations
- Consistent bridge method handling in annotation post-processors [SPR-12495] #17101 Consistent bridge method handling in annotation post-processors