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

Self autowiring does not use cglib for transactional and scheduled beans [SPR-16939] #21478

Closed
spring-projects-issues opened this issue Jun 14, 2018 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

Matej Spiller-Muys opened SPR-16939 and commented

We are using method with @Scheduled annotations inside beans that have @Transactional methods (including @Scheduled). The scheduled task is called using proxied been and the @Transactional works as expected.

However as soon as we add self autowiring (so we can manage @Transactional between method calls), then scheduled task method is called directly without proxy.

It does not work:

  1. settings property: spring.aop.proxy-target-class=true
  2. @EnableAspectJAutoProxy(proxyTargetClass = true)
  3. @EnableTransactionManagement(proxyTargetClass = true)

The only workaround is to manually add @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) to each problematic bean.
But this is error prone.

It should respect global proxy-target-class (it is default anyway in spring 5.0).

I have attached the example.

The same problem is in Spring 4.3.

Excepted stacktrace from @Scheduled:
...
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:284)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at springtest.MyServiceImpl$$EnhancerBySpringCGLIB$$ad7504a5.scheduled(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)

Actual stacktrace from @Scheduled:
java.lang.Exception
at springtest.MyServiceImpl.scheduled(MyServiceImpl.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)


Affects: 5.0.7

Attachments:

@spring-projects-issues spring-projects-issues added type: bug A general bug status: waiting-for-triage An issue we've not yet triaged or decided on and removed type: bug A general bug labels Jan 11, 2019
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@snicoll
Copy link
Member

snicoll commented Nov 12, 2023

That's because you inject the raw instance and self-references like that can't have the semantics you expect them to have. If you insist on using self-references (please don't) you need to inject an ObjectProvider or use @Lazy to relax the requirements and give the context a chance to do what you expect.

See also #28299

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2023
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 12, 2023
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: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants