Because the root class (CustomerServiceImpl) has some @Transactional methods, Spring will proxy instances of it. The default proxying method is to only proxy interfaces and not implementations, so it will never work to try to cast an injected blCustomerService to MyCustomerServiceImpl (just like you could never cast it to CustomerServiceImpl).
Instead, if you want to expose some things from MyCustomerServiceImpl you will have to put them on the MyCustomerService interface and just cast to that.
You could also just inject the MyCustomerService bean like this:
Code: Select all
public class SomeBean {
@Resource(name = "blCustomerService")
protected MyCustomerService customerService;
}
but you cannot do something like this:
Code: Select all
public class SomeBean {
@Resource(name = "blCustomerService")
protected MyCustomerServiceImpl customerService;
}
If you really want Spring to proxy implementations, you can tell Spring to use CGLIB instead of normal JDK dynamic proxies by adding this to any applicationContext.xml:
Code: Select all
<tx:annotation-driven proxy-target-class="true"/>
You will also need CGLIB as a dependency:
Code: Select all
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
I think the reason that Spring uses JDK proxying OOB because of portability (it's baked into the JDK).