简体   繁体   中英

Cast a class to interface within spring

I am hitting a wall with a ClassCastException, wondering what I do wrong. I have an API interface

public interface MyService{
    method A()
    method B()
}

implemented by a class

public class ServiceImpl implements MyService{
     //methods here
}

extended by another:

@Component("service")
public class ExtendedServiceImpl extends ServiceImpl
{
    //overriden methods
}

and in spring-components.xml

<osgi:service ref="service"
    interface="my.package.api.MyService" />

Now in another plugin I would like to have access to the service so I am invoking

ServiceReference s_reference = context.getServiceReference(MyService.class.getName());
MyService s=(MyService)context.getService(s_reference);

which throws ClassCastException. I can see by console printing s_reference is of type ExtendedServiceImpl which I don't have reference in that plugin.

Edit: Trimmed manifest of client:

Export-Service: org.hibernate.boot.registry.selector.StrategyRegistrationProvider
Import-Package: com.atlassian.confluence.plugins.questions.overriden;version="1.0.469", com.atlassian.confluence.plugins.questions.service;version="1.0.469"

Trimmed manifest of service

Bnd-LastModified: 1412434741719
Bundle-ClassPath: .,META-INF/lib/aspectjweaver-1.8.2.jar,META-INF/lib/
 atlassian-cache-compat-1.0.1.jar,META-INF/lib/atlassian-pocketknife-d
 ynamic-modules-0.20.jar,META-INF/lib/confluence-questions-api-1.0.469
 .jar,META-INF/lib/jooq-3.3.1.jar,META-INF/lib/jsp-api-2.0.jar
Bundle-Description: Confluence Questions
Bundle-DocURL: http://www.atlassian.com/
Bundle-License: http://www.atlassian.com/end-user-agreement/
Bundle-ManifestVersion: 2
Bundle-Name: Confluence Questions Plugin
Bundle-SymbolicName: com.atlassian.confluence.plugins.confluence-quest
 ions
Bundle-Vendor: Atlassian
Bundle-Version: 1.0.469
Export-Package: 

com.atlassian.confluence.plugins.questions.api.model,
com.atlassian.confluence.plugins.questions.overriden.admin,
com.atlassian.confluence.plugins.questions.overriden;uses:="com.atlassian.bandana,
com.atlassian.confluence.setup.bandana,
com.atlassian.spring.container,com.atlassian.confluence.plugins.questions.service,
com.atlassian.confluence.plugins.questions.api.service,
org.springframework.beans.factory.annotation,
com.atlassian.confluence.plugins.questions.api.model,
com.atlassian.confluence.plugins.questions.api.repository,
com.atlassian.confluence.plugins.questions.service.mapper,
com.atlassian.event.api,
com.atlassian.confluence.plugins.questions.api.permission,
com.atlassian.confluence.user,
com.atlassian.sal.api.message,
com.atlassian.confluence.plugins.questions.api.dto,
org.springframework.stereotype,
com.atlassian.user,
com.atlassian.hibernate,
com.atlassian.bonnie.analyzer,
com.atlassian.confluence.plugins.search.api";version="1.0.469",
com.atlassian.confluence.plugins.questions.api.service

At start i thought there was some inheritance problem thus I used sample classes. The MyService inteface lays at com.atlassian.confluence.plugins.questions.api.service The ServiceImpl lays at com.atlassian.confluence.plugins.questions.service The ExtendedServiceImpl lays at com.atlassian.confluence.plugins.questions.overriden

Hope it helps :)

Your code seems to be correct. The problem is about managing you package imports / exports.

This typically happens when there are two instances of the class MyService.class. Take a look into the bundle offering the service as well as the one using the service.

If you have no separate API bundle then the bundle offering the service should contain the MyService.class and export the package it is in. The bundle using the service should NOT contain the MyService.class and import the package it is in.

Also make sure that the packages of MyService.class is in a separate package from the impl and the using class - I think it should be the case already as your package name sounds like an API package name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM