Портлет Liferay 7 (DXP) Spring MVC, проблема с доступом к сервисам, созданным сервис-билдером

Я столкнулся с проблемой при доступе к службам (созданным с помощью проекта модуля построения служб) из портлета Spring MVC, я прочитал ссылку на форумы https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/service-trackers и попытался использовать Service Tracker для доступа обслуживание от контроллера.

здесь SpringTestPortletViewController.java

@Controller
@RequestMapping("VIEW")
public class SpringTestPortletViewController {

    private FooServiceTracker fooServiceTracker;
    @PostConstruct 
    public void init(){
         fooServiceTracker=new FooServiceTracker(this); 
         fooServiceTracker.open(); 
    }
    @PreDestroy public void destroy(){ 
         fooServiceTracker.close(); 
    }

    @RenderMapping
    public String question(Model model) {
        try {
            if(!fooServiceTracker.isEmpty()){
                FooLocalService fooLocalService=fooServiceTracker.getService();
                fooLocalService.saveFoo(); 
                System.out.println("----" + FooLocalServiceUtil.getFoosCount());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "SpringTest/view";
    }
}

FooServiceTracker.java

public class FooServiceTracker extends ServiceTracker<FooLocalService,FooLocalService>{

    public FooServiceTracker(Object host){
        super(FrameworkUtil.getBundle(host.getClass()).getBundleContext(), FooLocalService.class, null);
    }
}

Столкнувшись с проблемой ниже, во время развертывания портлета Spring MVC. Невозможно получить пакет, т.е. FrameworkUtil.getBundle(SpringTestPortletViewController.getClass()) становится нулевым

11:49:48,432 INFO  [Refresh Thread: Equinox Container: 6095db50-baf9-0016-1a03-97f126c9b821][PortletHotDeployListener:202] Registering portlets for SpringTest-portlet
11:49:48,649 ERROR [Refresh Thread: Equinox Container: 6095db50-baf9-0016-1a03-97f126c9b821][DispatcherPortlet:279] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springTestPortletViewController': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.portlet.FrameworkPortlet.createPortletApplicationContext(FrameworkPortlet.java:368)
    at org.springframework.web.portlet.FrameworkPortlet.initPortletApplicationContext(FrameworkPortlet.java:297)
    at org.springframework.web.portlet.FrameworkPortlet.initPortletBean(FrameworkPortlet.java:271)
    at org.springframework.web.portlet.GenericPortletBean.init(GenericPortletBean.java:124)
    at javax.portlet.GenericPortlet.init(GenericPortlet.java:136)
    at com.liferay.portlet.InvokerPortletImpl.init(InvokerPortletImpl.java:296)
    at com.liferay.portlet.PortletInstanceFactoryImpl.init(PortletInstanceFactoryImpl.java:294)
    at com.liferay.portlet.PortletInstanceFactoryImpl.create(PortletInstanceFactoryImpl.java:193)
    at com.liferay.portal.kernel.portlet.PortletInstanceFactoryUtil.create(PortletInstanceFactoryUtil.java:49)
    at com.liferay.portlet.PortletBagFactory.create(PortletBagFactory.java:217)
    at com.liferay.portal.service.impl.PortletLocalServiceImpl.initWAR(PortletLocalServiceImpl.java:858)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:153)
    at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:174)
    at com.sun.proxy.$Proxy123.initWAR(Unknown Source)
    at com.liferay.portal.kernel.service.PortletLocalServiceUtil.initWAR(PortletLocalServiceUtil.java:327)
    at com.liferay.portal.deploy.hot.PortletHotDeployListener.doInvokeDeploy(PortletHotDeployListener.java:210)
    at com.liferay.portal.deploy.hot.PortletHotDeployListener.invokeDeploy(PortletHotDeployListener.java:95)
    at com.liferay.portal.deploy.hot.HotDeployImpl.doFireDeployEvent(HotDeployImpl.java:237)
    at com.liferay.portal.deploy.hot.HotDeployImpl.fireDeployEvent(HotDeployImpl.java:104)
    at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil.java:28)
    at com.liferay.portal.kernel.servlet.PluginContextListener.fireDeployEvent(PluginContextListener.java:164)
    at com.liferay.portal.kernel.servlet.PluginContextListener.doPortalInit(PluginContextListener.java:154)
    at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycle.java:44)
    at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:74)
    at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:58)
    at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BasePortalLifecycle.java:54)
    at com.liferay.portal.kernel.servlet.PluginContextListener.contextInitialized(PluginContextListener.java:116)
    at com.liferay.portal.kernel.servlet.SecurePluginContextListener.contextInitialized(SecurePluginContextListener.java:151)
    at com.liferay.portal.osgi.web.wab.extender.internal.adapter.ServletContextListenerExceptionAdapter.contextInitialized(ServletContextListenerExceptionAdapter.java:51)
    at sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.equinox.http.servlet.internal.registration.ListenerRegistration$EventListenerInvocationHandler.invoke(ListenerRegistration.java:132)
    at com.sun.proxy.$Proxy491.contextInitialized(Unknown Source)
    at org.eclipse.equinox.http.servlet.internal.context.ContextController.doAddListenerRegistration(ContextController.java:359)
    at org.eclipse.equinox.http.servlet.internal.context.ContextController.addListenerRegistration(ContextController.java:312)
    at org.eclipse.equinox.http.servlet.internal.customizer.ContextListenerTrackerCustomizer.addingService(ContextListenerTrackerCustomizer.java:67)
    at org.eclipse.equinox.http.servlet.internal.customizer.ContextListenerTrackerCustomizer.addingService(ContextListenerTrackerCustomizer.java:1)
    at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:941)
    at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:1)
    at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229)
    at org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:901)
    at org.eclipse.osgi.internal.serviceregistry.FilteredServiceListener.serviceChanged(FilteredServiceListener.java:109)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.dispatchEvent(BundleContextImpl.java:917)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
    at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.publishServiceEventPrivileged(ServiceRegistry.java:862)
    at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.publishServiceEvent(ServiceRegistry.java:801)
    at org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl.register(ServiceRegistrationImpl.java:127)
    at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.registerService(ServiceRegistry.java:225)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.registerService(BundleContextImpl.java:464)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.registerService(BundleContextImpl.java:482)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.registerService(BundleContextImpl.java:1001)
    at com.liferay.portal.osgi.web.wab.extender.internal.WabBundleProcessor.initListeners(WabBundleProcessor.java:526)
    at com.liferay.portal.osgi.web.wab.extender.internal.WabBundleProcessor.init(WabBundleProcessor.java:153)
    at com.liferay.portal.osgi.web.wab.extender.internal.WebBundleDeployer._initWabBundle(WebBundleDeployer.java:186)
    at com.liferay.portal.osgi.web.wab.extender.internal.WebBundleDeployer.doStart(WebBundleDeployer.java:106)
    at com.liferay.portal.osgi.web.wab.extender.internal.WabFactory$WABExtension.start(WabFactory.java:163)
    at org.apache.felix.utils.extender.AbstractExtender.createExtension(AbstractExtender.java:259)
    at org.apache.felix.utils.extender.AbstractExtender.modifiedBundle(AbstractExtender.java:232)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:482)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:1)
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:232)
    at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:444)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.dispatchEvent(BundleContextImpl.java:905)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEventPrivileged(EquinoxEventPublisher.java:165)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:75)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:67)
    at org.eclipse.osgi.internal.framework.EquinoxContainerAdaptor.publishModuleEvent(EquinoxContainerAdaptor.java:102)
    at org.eclipse.osgi.container.Module.publishEvent(Module.java:461)
    at org.eclipse.osgi.container.Module.start(Module.java:452)
    at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:454)
    at org.eclipse.osgi.container.ModuleContainer.applyDelta(ModuleContainer.java:717)
    at org.eclipse.osgi.container.ModuleContainer.resolveAndApply(ModuleContainer.java:491)
    at org.eclipse.osgi.container.ModuleContainer.resolve(ModuleContainer.java:437)
    at org.eclipse.osgi.container.ModuleContainer.refresh(ModuleContainer.java:955)
    at org.eclipse.osgi.container.ModuleContainer$ContainerWiring.dispatchEvent(ModuleContainer.java:1336)
    at org.eclipse.osgi.container.ModuleContainer$ContainerWiring.dispatchEvent(ModuleContainer.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: java.lang.NullPointerException
    at com.service.tracker.FooServiceTracker.<init>(FooServiceTracker.java:11)
    at SpringTest.SpringTestPortletViewController.init(SpringTestPortletViewController.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:305)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 98 more

Оцените некоторые входные данные здесь,


person Ankit P    schedule 23.02.2017    source источник
comment
Если кто-то следит за этим сообщением, он также может найти ответ здесь web.liferay .com/community/forums/-/message_boards/message/   -  person Ankit P    schedule 24.02.2017


Ответы (3)


Реализация FrameworkworkUtil использует среду выполнения OSGI для загрузки классов. Я полагаю, что ваш класс не был загружен загрузчиком классов пакета OSGi, вместо этого он был загружен в AppClassLoader. getBundle возвращает только классы, загруженные через загрузчик классов OSGI, и, таким образом, возвращает NULL. Пожалуйста, обратитесь к org.osgi.framework.launch.FrameworkFactory документации, чтобы узнать больше о настройке среды выполнения OSGI.

person YuVi    schedule 23.02.2017
comment
согласен, но именно так это упоминается в dev.liferay .com/develop/tutorials/-/knowledge_base/7-0/. Я искал способ разрешить зависимость созданного построителем сервиса jar от портлета Spring MVC. - person Ankit P; 24.02.2017

Общая практика, которой я следовал в своем опыте, заключалась в том, чтобы поддерживать общие файлы JAR построителя сервисов, такие как пользовательские сервисы и т. д., в tomcat/lib/ и хранить JAR-файлы построителя сервисов, специфичные для портлета, в WEB-INF/lib/ портлета. Кроме того, если есть необходимость в совместном использовании jar-файлов построителя, все равно поместите их в портлет WEB-INF/lib и используйте required-deployment-contexts для ссылки на jar-файл в другом контексте портлета.

person YuVi    schedule 24.02.2017
comment
Спасибо, он работал до версии 6.2, но работает ли установка jar модуля в портлете WEB-INF/lib/ или использование требуемых контекстов развертывания для указания проекта API модуля в портлете DXP Spring MVC? Поскольку контекст для Spring Portlet, т.е. модуля WAB и OSGi для построителя сервисов, отличается? - person Ankit P; 27.02.2017

Я переместил файлы API модуля построителя сервисов в tomcat/lib/ext, перезапустил сервер, а затем получил доступ к классу Util в портлете Spring MVC, который сработал. Но вам нужно какое-то руководство для лучших практик, чтобы обрабатывать такие зависимости jar-файлов модуля построителя сервисов в портлете Spring MVC?

person Ankit P    schedule 24.02.2017