Skip to content

Bridged method detection leads to false positive detection of autowired annotations [SPR-8660] #13302

@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions