[英]Get osgi service reference from pure osgi bundle in Liferay portlet
I have successfully register the org.amdatu.mongo service in apche felix like shown below, 我已经在apche felix中成功注册了org.amdatu.mongo服务,如下所示,
Bundle is shown below which export the service which is in active/running state. 捆绑软件显示在下面,导出处于活动/运行状态的服务。
Now I want to use this service in my portlet and I have return below code, 现在,我想在我的portlet中使用此服务,并且返回以下代码,
package com.example.portlet;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import java.io.IOException;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.amdatu.mongo.MongoDBService;
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.osgi.service.component.annotations.Component;
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=loveworld Portlet",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class LoveworldmvcportletPortlet extends MVCPortlet {
@ServiceDependency
private volatile MongoDBService m_mongoDbService;
@Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
System.out.println( m_mongoDbService);//**getting NULL**
super.doView(renderRequest, renderResponse);
}
}
I have tried with annotation @ServiceDependency no luck getting null.My mongoDb server is running fine!! 我已经尝试过使用注解@ServiceDependency来获取null了,我的mongoDb服务器运行正常!
I have Menifest.mf file as below, 我有如下的Menifest.mf文件,
Manifest-Version: 1.0
Bnd-LastModified: 1469380000381
Bundle-ManifestVersion: 2
Bundle-Name: loveworld
Bundle-SymbolicName: loveworld
Bundle-Version: 1.0.0
Created-By: 1.8.0_51 (Oracle Corporation)
Import-Package: com.liferay.portal.kernel.portlet.bridges.mvc;version=
"[1.0,2)",javax.portlet;version="[2.0,3)",javax.servlet,javax.servlet
.http,org.amdatu.mongo;version="1.0.0"
Javac-Debug: on
Javac-Deprecation: off
Javac-Encoding: UTF-8
Private-Package: com.example.portlet,content
Provide-Capability: osgi.service;objectClass:List<String>="javax.portl
et.Portlet",liferay.resource.bundle;bundle.symbolic.name=loveworld;re
source.bundle.base.name="content.Language"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=jsp.taglib
)(uri=http://java.sun.com/portlet_2_0))",osgi.extender;filter:="(&(os
gi.extender=jsp.taglib)(uri=http://liferay.com/tld/aui))",osgi.extend
er;filter:="(&(osgi.extender=jsp.taglib)(uri=http://liferay.com/tld/p
ortlet))",osgi.extender;filter:="(&(osgi.extender=jsp.taglib)(uri=htt
p://liferay.com/tld/theme))",osgi.extender;filter:="(&(osgi.extender=
jsp.taglib)(uri=http://liferay.com/tld/ui))",osgi.ee;filter:="(&(osgi
.ee=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/com.example.portlet.LoveworldmvcportletPor
tlet.xml
Tool: Bnd-3.2.0.201605172007
bnd.bnd file is as below, bnd.bnd文件如下,
Bundle-SymbolicName: loveworld
Bundle-Version: 1.0.0
Import-Package: com.liferay.portal.kernel.portlet.bridges.mvc;version="[1.0,2)",javax.portlet;version="[2.0,3)",javax.servlet,javax.servlet.http,org.amdatu.mongo;version="1.0.0"
Portlet which consume the service is as shown below, 使用该服务的Portlet如下所示,
You are using the following two annotations: 您正在使用以下两个注释:
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.osgi.service.component.annotations.Component;
One of them is the standard OSGi annotation for Declarative Services components, the other one from the Felix Dependency Manager project. 其中之一是用于声明式服务组件的标准OSGi批注,另一种是来自Felix Dependency Manager项目的。 Be sure that you use annotations only from org.osgi.service.component.annotations package.
确保仅使用org.osgi.service.component.annotations包中的注释。
Also, you should add a bind method to your component for the service reference. 另外,您应该在组件中添加一个绑定方法以用于服务引用。
First problem: the service provided by your bundle is of type ManagedServiceFactory
. 第一个问题:捆绑软件提供的服务的类型为
ManagedServiceFactory
。 You are trying to inject a service of type MongoDBService
. 您正在尝试注入
MongoDBService
类型的服务。 This obviously doesn't match. 这显然不匹配。
Second problem: you should not be trying to directly consume ManagedServiceFactory
yourself... it is part of the Config Admin specification, and should only be consumed by Config Admin itself. 第二个问题:您不应该自己尝试直接使用
ManagedServiceFactory
...它是Config Admin规范的一部分,并且只能由Config Admin本身使用。
Third problem: as Balazs points out, you are using annotations from a mixture of different frameworks: Declarative Services (DS) and Dependency Manager (DM). 第三个问题:正如Balazs指出的那样,您正在使用来自以下不同框架的注释:声明性服务(DS)和依赖管理器(DM)。 These cannot be mixed within a single component... pick one!
这些不能混合在单个组件中...选择一个!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.