Zeeshan
Zeeshan

Reputation: 1183

Spring Bean implements interface

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

Upvotes: 1

Views: 4859

Answers (1)

gresdiplitude
gresdiplitude

Reputation: 1685

Proxying is how Spring implements declarative transaction management. Spring reference is the best place for all your questions on this.

The most important concepts to grasp with regard to the Spring Framework's declarative transaction support are that this support is enabled via AOP proxies, and that the transactional advice is driven by metadata (currently XML- or annotation-based). The combination of AOP with transactional metadata yields an AOP proxy that uses a TransactionInterceptor in conjunction with an appropriate PlatformTransactionManager implementation to drive transactions around method invocations.

and on <aop:aspectj-autoproxy proxy-target-class="true"/>

The proxy-target-class attribute on the element controls what type of transactional proxies are created for classes annotated with the @Transactional annotation. If proxy-target-class attribute is set to true, class-based proxies are created. If proxy-target-class is false or if the attribute is omitted, standard JDK interface-based proxies are created. (See Section 8.6, “Proxying mechanisms” for a discussion of the different proxy types.)

Upvotes: 1

Related Questions