2014-09-07 3 views
1

Liferay 6.2에서 Spring 4.0.6을 사용하고 있습니다. Spring은 autowired 컴포넌트를 후크에 삽입 할 수 없으며 객체는 null로 제공됩니다. 나는 또한 liferay와 함께 제공되는 봄 버전 3.1을 사용해 보았습니다. 동일한 코드가 포틀릿에서 작동하지만 후크에서는 작동하지 않습니다.Liferay 6.2 + Spring @Autowired가 후크에서 작동하지 않습니다.

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

어떤 도움이 많이 이해할 수있을 것이다.

감사

답변

5

불행하게도, autowire가 메커니즘은 후크, 래퍼 또는을 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()); 
     {...} 
} 

는 희망이 도움이!

+1

멋진 ... 감사합니다! (전체 Spring 구성 전에 실행 되었기 때문에 메소드 내에 프록시 선언을 넣기 만하면됩니다.) –

관련 문제