1

我正在使用带有 Liferay 6.2 的 Spring 4.0.6。Spring 无法将自动装配的组件注入到钩子中,对象为空。我也尝试过 liferay 附带的 spring 3.1 版。相同的代码在 portlet 中有效,但在钩子中无效。

ActivityEventPublisher.java 中的私有 ApplicationEventPublisher 发布者为空。

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);
}
}

任何帮助都感激不尽。

谢谢

4

1 回答 1

6

不幸的是,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;
    }
}

在 Hook 中使用的示例如下:

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());
        {...}
}

希望能帮助到你!

于 2014-09-08T16:04:19.893 回答