2014-10-04 60 views
0

我用ClassCastException打墙,想知道我做错了什么。 我有一个API接口在春天浇铸一个接口类

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

由类

public class ServiceImpl implements MyService{ 
    //methods here 
} 

延伸通过另一个实施:

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

和在弹簧的components.xml现在

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

在另一个插件在我想获得服务,所以我正在调用

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

它抛出ClassCastException。我通过控制台打印可以看到s_reference是ExtendedServiceImpl类型,我没有在该插件中引用它。

编辑: 修剪清单客户端:

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" 

修剪舱单服务

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 

在开始我认为有这样一些问题,继承我用样品的类。 MyService接口奠定了com.atlassian.confluence.plugins.questions.api.service ServiceImpl奠定了com.atlassian.confluence.plugins.questions.service ExtendedServiceImpl奠定了com.atlassian.confluence.plugins.questions。重写

希望它有帮助:)

+0

请同时发布完整的堆栈跟踪。 – John 2014-10-04 08:39:15

+0

'getServiceReference(Service.class.getName());',你在这里使用了什么'Service'? [This one](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Service.html)? – 2014-10-04 08:42:38

+0

也许应该是MyService.class而不是Service.class? – John 2014-10-04 08:45:37

回答

1

您的代码似乎是正确的。问题在于管理你的包装进口/出口。

这通常发生在类MyService.class有两个实例时。查看提供该服务的软件包以及使用该服务的软件包。

如果你没有单独的API包,那么提供服务的包应该包含MyService.class并导出它所在的包。使用该服务的包不应包含MyService.class并导入它所在的包

还要确保MyService.class的包与impl和using类不在同一个包中 - 我认为应该是这样,因为包名听起来像是一个API包名。

+0

我记得在某处阅读了您描述的这两个实例问题。MyService,ServiceImpl和ExtendedServiceImpl在同一个项目下,而我试图从另一个项目中获取服务。您是否会善意指出您提到的这些包装进口/出口的文件? – 2014-10-04 09:59:03

+0

我假设你使用maven bundle插件来创建bundle。在这种情况下,请参阅maven bundle插件的文档:http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html 您还应该检查创建的jar的Manifest文件为您的服务和客户端。如果你想要,你可以在这里发布Manifest内容,我看看。 – 2014-10-04 10:42:01

+0

我在你指出的pom导入/导出上做了一些改动(很多感谢btw),现在让构造函数抛出异常;嵌套异常是org.springframework.aop.AopInvocationException:AOP配置似乎是无效的:尝试调用方法嵌套异常是java.lang.IllegalArgumentException:对象不是声明类的实例。我在编辑原始问题以包含清单 – 2014-10-04 13:59:27