Results 1 to 2 of 2

Thread: Why spring uses proxy for the Bean which implements interface...

  1. #1

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

    Code:
        public interface Service {
           
          public void doSomething();
        }
    
        @Service
        public class MyService implements service{
    
              @Transactional 
              public void doSomething(){
              }
        }
        
        @Controller
        public class MyController {
        
          @Autowired
          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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    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

Tags for this Thread

Posting Permissions

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