Announcement Announcement Module
Collapse
No announcement yet.
want to inject subclass of 'real' class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • want to inject subclass of 'real' class

    I have heard that one reason to use dependency injection is to allow different implementations of an interface to be used with a configuration change, such as replacing a production class with one for testing.

    So my webservice has another webservice that it uses, and a class to implement the interface, etc. I decided to extend the production class and add testing things to the subclass; then all I had to do (I thought) was remove the annotations configuring the production class as the bean and put those onto the subclass, and I'd be in business.

    Spring gets upset with this and I don't understand why.

    The original implementation class has (after commenting out its annotations):

    Code:
    //@ManagedResource(objectName = "bean:name=XServiceMBean")
    //@Service("xService")
    public class XServiceImpl implements XService
    {
    // working code here
    }
    and my extension is:

    Code:
    @ManagedResource(objectName = "bean:name=XServiceMBean")
    @Service("xService")
    public class XServiceImplTest extends XServiceImpl
    {
    // working code here
    }
    and my error message:

    Error creating bean with name 'xService':Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequire dTypeException: Bean named 'xService' must be of type [x.y.z.XService], but was actually of type [x.y.z.w.XServiceImplTest]

    So I'm confused. It seems to me that XServiceImplTest is of type XService, by virtue of implementing that interface, by virtue of extending another class that does. I'd appreciate any insights into why this doesn't work.
Working...
X