Liferay 如果我引用服务,Gogo Shell命令将消失 源代码
在我的模块中,我编写了以下Liferay服务:Liferay 如果我引用服务,Gogo Shell命令将消失 源代码,liferay,osgi,liferay-7,gogo-shell,Liferay,Osgi,Liferay 7,Gogo Shell,在我的模块中,我编写了以下Liferay服务: package jp.co.my.module; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.module.framework.service.IdentifiableOSGiService; import com.liferay.portal.kernel.service.BaseLocalServiceI
package jp.co.my.module;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.module.framework.service.IdentifiableOSGiService;
import com.liferay.portal.kernel.service.BaseLocalServiceImpl;
public class TestService extends BaseLocalServiceImpl
implements IdentifiableOSGiService {
public void test() throws PortalException {
System.out.println("In the future I will do stuff here");
}
@Override
public String getOSGiServiceIdentifier() {
return TestService.class.getName();
}
}
在同一模块中,我编写了一个使用该服务的命令:
package jp.co.my.module;
import com.liferay.portal.kernel.exception.PortalException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(
property = {
"osgi.command.function=test",
"osgi.command.scope=liferay"
},
service = Object.class)
public class TestCommand {
public void test() throws PortalException {
System.out.println("In the future I will call the service here");
}
@Reference private volatile TestService _testService;
}
问题
模块正确部署,但在Gogo Shell中,该命令不可用:
g! test
gogo: CommandNotFoundException: Command not found: test
g! b 1001
jp.co.my.module_1.0.0 [1001]
Id=1001, Status=ACTIVE Data Root=/home/nico/liferay/osgi/state/org.eclipse.osgi/1001/data
"No registered services."
Services in use:
{org.osgi.service.log.LogService, org.eclipse.equinox.log.ExtendedLogService}={service.id=2, service.bundleid=0, service.scope=bundle}
No exported packages
Imported packages
com.liferay.portal.kernel.exception; version="7.1.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
com.liferay.portal.kernel.module.framework.service; version="1.0.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
com.liferay.portal.kernel.service; version="1.27.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
No fragment bundles
No required bundles
问题:为什么?如何正确引用该服务?我没有使用liferay,但它似乎非常符合OSGI的概念。TestCommand是一个依赖于TestService的组件,因为您使用了@Reference。这意味着TestService也是一个组件。但您还没有将TestService定义为任何地方的组件 我有经验的猜测是TestService也必须使用@Component,这样liferay就会知道它是一个组件,创建它,并将它注入TestCommand:
@Component
public class TestService extends BaseLocalServiceImpl
implements IdentifiableOSGiService {
我不知道,在没有定义java接口的情况下尝试创建和引用组件可能会有其他问题
编辑:我的答案反映了一些信息
EDIT2:它在没有行的情况下工作得很好的原因是删除@Reference会删除TestCommand和TestService之间断开的依赖关系链接。有了这行代码,依赖链就在那里,但没有解析,所以TestCommand不会启动
编辑3:
@Component(
configurationPid = "jp.co.my.module",
immediate = true,
property = {},
service = TestService.class
)
非常感谢,这确实是问题所在!我冒昧地为您的答案添加了完整的注释,请随意编辑。
@Component(
configurationPid = "jp.co.my.module",
immediate = true,
property = {},
service = TestService.class
)