Announcement Announcement Module
No announcement yet.
selectively proxy target class Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • selectively proxy target class

    Hi all,

    I am using @Transactional to proxy classes with transactional behavior. Therefore my spring context contains this:
    <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:
    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:

    <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?)


  • #2
    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.