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

AOP auto-proxying with proxyTargetClass=true and introduction advice does not work for JDK proxy targets #27044

Closed
garyrussell opened this issue Jun 9, 2021 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@garyrussell
Copy link
Contributor

See spring-projects/spring-retry#214 (comment)

Spring retry uses AOP auto proxy to add a proxy for @Retryable methods; the code unconditionally wraps the target in a new proxy. If proxyTargetClass is true (default in Spring Boot), the AbstractAutoProxyCreator does not add interfaces if the target is a JDK proxy.

if (!proxyFactory.isProxyTargetClass()) {
	if (shouldProxyTargetClass(beanClass, beanName)) {
		proxyFactory.setProxyTargetClass(true);
	}
	else {
		evaluateProxyInterfaces(beanClass, proxyFactory);
	}
}

This causes, for example, @Retryable on a spring data repository to create an invalid bean

@Repository
public interface TestRepository extends CrudRepository<Test, String> {

	@Override
	@Retryable(maxAttempts = 4)
	List<Test> findAll();

}

The inner (spring-data) proxy implements TestRepository, etc, but the outer proxy only implements the Retryable interceptor interface.

The bean 'testRepository' could not be injected as a 'com.example.demo.TestRepository' because it is a JDK dynamic proxy that implements:
	org.springframework.data.repository.CrudRepository

Although the error message is wrong (spring-projects/spring-boot#26821), it is because the proxy only implements Retryable.

Perhaps the AbstractAutoProxyCreator could detect that the target is a JDK proxy and copy the interfaces (even when proxyTargetClass) is true?

@jhoeller jhoeller self-assigned this Jun 9, 2021
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Jun 9, 2021
@jhoeller jhoeller added this to the 5.3.9 milestone Jun 9, 2021
@jhoeller jhoeller changed the title AOP AutoProxy Missing Interfaces with proxyTargetClass=true and the target is already a proxy AOP auto-proxying with proxyTargetClass=true and introduction advice does not work for JDK proxy targets Jul 8, 2021
@jhoeller jhoeller added type: bug A general bug and removed type: enhancement A general enhancement labels Jul 8, 2021
@jhoeller
Copy link
Contributor

jhoeller commented Jul 8, 2021

This seems to fail only in case of an introduction advice, where our fallback check in DefaultAopProxyFactory is misled into assuming that an interface has been configured (even if it was just an introduced interface), unfortunately skipping the fallback in that case. I've revised AbstractAutoProxyCreator to specifically handle JDK proxy targets in such a scenario, explicitly adding their original interfaces.

@jhoeller jhoeller added the for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x label Jul 8, 2021
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x labels Jul 8, 2021
jhoeller added a commit that referenced this issue Jul 13, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
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: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants