Announcement Announcement Module
No announcement yet.
Why spring uses proxy for the Bean which implements interface... Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why spring uses proxy for the Bean which implements interface...

        public interface Service {
          public void doSomething();
        public class MyService implements service{
              public void doSomething(){
        public class MyController {
          private MyService service;
    In above scenario autowiring fails with exception "illegalArgumentException : argument type mismatch". When i remove `implements service` from MyService everything works fine.

    I have searched and found that place `<aop:aspectj-autoproxy proxy-target-class="true"/>` in applicationContext.xml for successful autowiring. and it worked.
    I have also found that spring uses JDK proxy when @Transactional is used.

    I have some confusions,
    1. How @Transactional relates to Proxying
    2. Why spring uses JDK Proxy for the beans which implements interfaces.
    3. Why i need to place `<aop:aspectj-autoproxy proxy-target-class="true"/>` in applicationContext.xml

    Can anyone please explain ? or refer me any article or blog

  • #2
    I suggest a read of the reference guide as your questions are addressed in there.

    In short for spring to apply aspects (AOP) (and yes transactions are one of those) spring uses proxies. If you don't specify which one spring does some detection itself and when there are interfaces uses JDK proxies else classbased proxies.

    However if you specify an interface your other code should also use this interface and not the concrete implementation class so basically your code using the dao/service is wrong.