Results 1 to 2 of 2

Thread: selectively proxy target class

  1. #1

    Default selectively proxy target class

    Hi all,

    I am using @Transactional to proxy classes with transactional behavior. Therefore my spring context contains this:
    Code:
    <tx:annotation-driven transaction-manager="myTransactionManager"/>
    However, in one case, the annotated class implements a certain interface, while I prefer to autowire that bean based on its class (the interface is meaningless in that particular context).

    Like so:
    Code:
    @Transactional
    class MyService implements SomeInterface
    {...}
    
    class Client
    {
    @autowired MyService service;
    }
    This fails, as the created proxy is of type SomeInterface, and not MyService.
    In order for the autowiring to work, I need to change my AOP config, like so:

    Code:
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" proxy-target-class="true"/>
    However, that would apply to all my beans, while I actually prefer to proxy by interface as a general rule.

    Is there a way to only apply the proxy-target-class to specific classes?

    (Or - is there a different way to make the @autowire pick-up the proxy?)

    Thanks,

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    If your class has an interface, and you don't force cglib class proxies with proxy-target-class="true", Spring will always choose jdk interface proxies for that class. So, to activate classproxies for that class (so that you can autowire by type), but not to all other classes, your only option would be to remove the interface(which is NOT recommended).

    My suggestion is: for that paticular class, autowire by name, or don't autowire at all. This way you can use jdk interface proxies. A little bit of xml configuration isn't always that bad if you need it.

Posting Permissions

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