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
{{ message }}
Repository navigation
Add support for mixed, fine-grained JDK- and CGLIB-based proxying [SPR-3665] #8346
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process
Aye Juergen, I know you tagged that issue as 'expected behaviour', and it was turned into a documentation issue that I addressed, but I (and others evidently) think that the proxying 'feature' of all JDK-proxies or all CGLIB-proxies is too coarse. It should be more fine grained than that, hence my raising this as an enhancement request. I'll have a dig into the code later on today and see exactly what is going on :)
Would it be possible to add some bean definition pre-processor that gets consulted by the proxy factory?
Basically a Strategy pattern, with a method
Boolean preserveTargetClass(BeanDefinition bd)
where a TRUE means use CGLib, FALSE means JDK proxy and null means "abstain, use default you have".
To avoid life cycle issues, perhaps you would only allow inner beans from within aop:auto-proxy element.
I have the following use case, connected to JAX-WS and the fact it is relying on annotations (I suppose you can have similar UC w/ other Tiger based technologies):
I normally like JDK proxy (proxyTarget=false)
JAX-WS runtime examines Java POJO implementing WebService for annotations which are not inherited by JDK proxy (since it is interface based) => proxyTarget=true
JAX-WS WebService client classes are based on JDK proxies themselves. Since proxies are final classes, applying CGLib here makes app go boom => proxyTarget=false
I have nice interfaces for my stuff. Having CGLib on anywhere gives warnings about "unable to proxy final class setDataSource()" for my DAO classes which is at best annoying => proxyTarget=false
Confusing & brittle? Not at all, Spring shields you from the complexity of all the stuff it does. I found it quite easy to get the stuff I needed into Spring infrastructure by just overriding one little method in the AutoProxyCreator.
This would be a good feature. In our case we are using Scala for newer code. This means it defaults to proxy objects as scala classes always implements scala object. If we set proxy target to true to makes the jersey resources register them properly. However old java code that is final classes will not work. Would be good if we can set only cglib for a particular config only.
This is kinda related to #13000 a controller or jersey resource that implements a single interface becomes a proxy object. So ClassUtils.getUserClass is unable to get the true object behind as it can only deal with cglib subclasses.
Spring Cloud and Spring Boot increasingly rub up against this because we provide aspects that are autoconfigured, and their behaviour alters the way user code works in unexpected and unintended ways, depending on whether proxy-target-class=true or false. E.g. with Spring Cloud Sleuth we intercept RestTemplate with an aspect, but we don't want to prevent users from declaring a bean of that type (which is what would happen if they didn't set proxy-target-class=true, but they might not want to do that if they have final classes anywhere, e.g. from legacy or third party libraries).
Just recently I ran into this when Boot 1.4 milestones activated proxyTargetClass true and all of a sudden @Transactional annotations applied on an interface stopped working.
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process
Activity
spring-projects-issues commentedon Sep 22, 2007
Jürgen Lukasczyk commented
Would it be possible to add some bean definition pre-processor that gets consulted by the proxy factory?
Basically a Strategy pattern, with a method
Boolean preserveTargetClass(BeanDefinition bd)
where a TRUE means use CGLib, FALSE means JDK proxy and null means "abstain, use default you have".
To avoid life cycle issues, perhaps you would only allow inner beans from within aop:auto-proxy element.
spring-projects-issues commentedon Sep 22, 2007
Taras Tielkes commented
What exactly is the use case for "more fine grained" proxying? The current infrastructure is already confusing and brittle enough, imho.
spring-projects-issues commentedon Sep 23, 2007
Jürgen Lukasczyk commented
I have the following use case, connected to JAX-WS and the fact it is relying on annotations (I suppose you can have similar UC w/ other Tiger based technologies):
Confusing & brittle? Not at all, Spring shields you from the complexity of all the stuff it does. I found it quite easy to get the stuff I needed into Spring infrastructure by just overriding one little method in the AutoProxyCreator.
spring-projects-issues commentedon Mar 14, 2012
Jun Yamog commented
This would be a good feature. In our case we are using Scala for newer code. This means it defaults to proxy objects as scala classes always implements scala object. If we set proxy target to true to makes the jersey resources register them properly. However old java code that is final classes will not work. Would be good if we can set only cglib for a particular config only.
This is kinda related to #13000 a controller or jersey resource that implements a single interface becomes a proxy object. So ClassUtils.getUserClass is unable to get the true object behind as it can only deal with cglib subclasses.
spring-projects-issues commentedon Jan 30, 2013
Mukarram Baig commented
We ran into the same scenarios as Jurgen mentioned and wanted to know if there are any workarounds for it?
spring-projects-issues commentedon Jun 16, 2016
Dave Syer commented
Spring Cloud and Spring Boot increasingly rub up against this because we provide aspects that are autoconfigured, and their behaviour alters the way user code works in unexpected and unintended ways, depending on whether proxy-target-class=true or false. E.g. with Spring Cloud Sleuth we intercept
RestTemplatewith an aspect, but we don't want to prevent users from declaring a bean of that type (which is what would happen if they didn't set proxy-target-class=true, but they might not want to do that if they have final classes anywhere, e.g. from legacy or third party libraries).spring-projects-issues commentedon Jun 16, 2016
Oliver Drotbohm commented
Just recently I ran into this when Boot 1.4 milestones activated
proxyTargetClasstrue and all of a sudden@Transactionalannotations applied on an interface stopped working.spring-projects-issues commentedon Jan 12, 2019
Bulk closing outdated, unresolved issues. Please, reopen if still relevant.