Liferay 6.2 + Spring @Autowired无法正常运行

Chirdeep Take

我将Spring 4.0.6和Liferay 6.2一起使用。Spring不能将自动装配的组件注入到钩子中,对象为null。我还尝试了Liferay附带的3.1版。相同的代码在portlet中有效,但在钩子中无效。

ActivityEventPublisher.java中的private ApplicationEventPublisher发布者为null。

web.xml

<?xml version="1.0"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-         app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>com.liferay.portal.kernel.servlet.SecurePluginContextListener</listener-  class>
</listener>
<listener>
<listener-class>com.liferay.portal.kernel.servlet.PortletContextListener</listener-class>
</listener>

<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
</web-app>

ActivityEventPublisher.java

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

import connect.activity.solr.document.ActivityData;

@Component
public class ActivityEventPublisher implements ApplicationEventPublisherAware {

private ApplicationEventPublisher publisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}

public ApplicationEventPublisher getPublisher() {
return publisher;
}

public void setPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}

public void publish(ActivityData data) {
ActivityEvent event = new ActivityEvent(this);
event.setActivityData(data);
this.publisher.publishEvent(event);
}
}

任何帮助都感激不尽。

谢谢

弗朗西斯科·哈维尔·巴雷纳

不幸的是,Liferay在挂钩,包装或启动操作中不允许使用自动装配机制。

您可以实现一个ApplicationContextProvider,它是如此简单和有用:

@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext ctx = null;

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        ctx = ac;
    }
}

在挂钩中使用的示例如下:

public class PostLoginActionHook extends Action {

// That code "replaces" @Autowired annotation
private final UserProxyService userProxyService = (UserProxyService) ApplicationContextProvider.
        getApplicationContext().getBean(UserProxyService.class);


@Override
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
        UserVO myCustomUser = userProxyService.getCustomUserByLiferayUser(user.getUserId());
        {...}
}

希望能帮助到你!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring @Autowired无法正常工作并返回null

来自分类Dev

Liferay portlet with freemarker and spring

来自分类Dev

Liferay Spring MVC

来自分类Dev

如何将Spring OAuth2服务器集成到Liferay

来自分类Dev

spring-boot-starter-web @Autowired无法正常工作

来自分类Dev

测试时Web Service中的Spring Autowired无法正常工作

来自分类Dev

Web Service中的Spring Autowired无法正常工作

来自分类Dev

(Spring-Batch)Autowired在StepScopeTestExecutionListener中无法正常工作

来自分类Dev

spring-boot-starter-web @Autowired无法正常工作

来自分类Dev

为什么Spring基本@Autowired无法正常工作?

来自分类Dev

liferay自动部署无法正常工作

来自分类Dev

liferay自动部署无法正常工作

来自分类Dev

liferay lucene搜索无法正常工作

来自分类Dev

简单的Liferay ProcessAction无法正常工作

来自分类Dev

Liferay无法处理模板?

来自分类Dev

无法让 Liferay 登录 UserId

来自分类Dev

简单的Spring Portlet无法在Liferay 6.2的动作映射上工作

来自分类Dev

Liferay配置操作类-Spring依赖注入

来自分类Dev

带Freemarker和Spring的Liferay Portlet

来自分类Dev

无法为 Liferay 7.2-rc2 创建工作区

来自分类Dev

注销在 Spring Security OAuth2 中无法正常工作

来自分类Dev

FadeIn动画无法正常运行2

来自分类Dev

无法创建Liferay插件项目

来自分类Dev

无法在Liferay上注册portlet

来自分类Dev

无法创建Liferay插件项目

来自分类Dev

服务器重启后Liferay Scheduler无法正常工作

来自分类Dev

使用Struts2的@Autowired Spring注释

来自分类Dev

如何在Liferay 6中建立索引并搜索“公共页面”?

来自分类Dev

liferay 6:从面包屑到达主要站点