Results 1 to 6 of 6

Thread: Force aspect to use CGLIB

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    Default Force aspect to use CGLIB

    Hi,

    after 2 days of googling around I could not find a definitive answer to my
    problem, so here it is:

    we have a large Spring based webapp, which uses Spring AOP in quite a few places. Up till now we lived our lives happily with using interface proxies
    applied using

    Code:
    <aop:aspectj-autoproxy/>
    Now we want to add some before-method advice to one of the Spring classes
    Code:
    org.springframework.transaction.support.TransactionTemplate

    The problem is that we need to use the CGLIB proxy instead of the JDK one.
    Now I know that we could just use
    Code:
     proxyTargetClass="true"
    but that would mean that ALL proxies would be CGLIB based and we don't want that.

    I've tried to mix aop:aspectj-autoproxy and ProxyFactoryBean with no success.

    Is it possible to force one single Aspect to use CGLIB?


    Thanks,
    dratewka

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Proxy creation should detect if it need cglib of jdk dynamic proxies... So basically there shouldn't be a problem,however the transactiontemplate implements some interfaces so it will automatically create a jdk dynamic proxy.

    Mixing strategies is in general not a great idea, it leads to proxying proxies and all short of other troubles.

    What you could do is for this single class create an aop:config element which only matches the TransactionTemplate and which is configured to use a class proxy, that should work.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2011
    Posts
    3

    Default

    Thank you Marten for your response!

    ... however the transactiontemplate implements some interfaces so it will automatically create a jdk dynamic proxy.
    this is exactly what happens - I get a proxy implementing two interfaces, which causes problems with code which is using TransactionTemplate objects.


    I haven't tried to create an aop:config element because the documentation states :
    To be clear: using 'proxy-target-class="true"' on <tx:annotation-driven/>, <aop:aspectj-autoproxy/> or <aop:config/> elements will force the use of CGLIB proxies for all three of them.
    So as I understand if I use an aop:config element with proxyTargetClass="true" then I would actually force all the proxies to be class based.

    Or am I wrong here? (I hope I am)

  4. #4
    Join Date
    Apr 2011
    Posts
    3

    Default

    Or maybe it's possible to subclass the proxy factory used by Spring and modify it?

    Is it possible to use such a homemade proxy factory instead of the original one?

  5. #5
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    12

    Default

    Have you tried the following for the intended classes?

    @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "MyIntendedScope")

    i.e annotating at the class level

  6. #6
    Join Date
    Jun 2006
    Posts
    12

    Default

    I am encountering this same issue; in my case I am trying to add advice to the Spring AMQP RabbitTemplate class which is injected into a class which extends Spring AMQP RabbitGatewaySupport. RabbitGatewaySupport has a property of type RabbitTemplate so my bean is inheriting this property. RabbitTemplate implements the RabbitOperations interface, so Spring is generating a JDK proxy and then failing when it attempts to inject this proxy into my class extending RabbitGatewaySupport.

    For anyone else that comes down this path, looks like this is https://jira.springsource.org/browse/SPR-3665. At the moment we are resorting to using CGLIB proxies, which has meant some changes to other proxied code, such as DAOs (CGLIB doesn't play well with constructor injection since a parameterless constructor is required).

    Any alternative suggestions as to how we might selectively proxy just this advise with CGLIB without affecting the rest of the application would be appreciated (I looked at the @Scope annotation but that appears to affect the proxy that is generated when using a scope which requires a proxy).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •