本文介绍了Liferay + Spring + Spring Web Mvc-@Autowired不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:生命射线6.1春天3.1spring-portlet-mvc

using:liferay 6.1spring 3.1spring-portlet-mvc

我们尝试使用@Autowired注入弹簧豆,看来这不适用于liferay 6.1吗?已知的错误?解决方法?

We tried to inject spring beans with @Autowired and it seems that this doesnt work with liferay 6.1 ? Known bug? Workaround?

这是我的applicationContext.xml:

here my applicationContext.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<bean id="viewNameTranslator"
      class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>

<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.ebcont.yatc.portlets.service"/>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:configuration.properties</value>
            <value>classpath:mail.properties</value>
        </list>
    </property>
</bean>

<bean id="appProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">

    <property name="singleton" value="true"/>

    <property name="properties">
        <props>
            <prop key="application.name">${application.name}</prop>
        </props>
    </property>

</bean>

<bean id="mailProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">

    <property name="singleton" value="true"/>

    <property name="properties">
        <props>
            <prop key="mail.host">${mail.host}</prop>
            <prop key="mail.port">${mail.port}</prop>
            <prop key="mail.username">${mail.username}</prop>
            <prop key="mail.password">${mail.password}</prop>
            <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
            <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
            <prop key="mail.registration.sender">${mail.registration.sender}</prop>
            <prop key="mail.registration.subject">${mail.registration.subject}</prop>
        </props>
    </property>

</bean>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.host}"/>
    <property name="port" value="${mail.port}"/>
    <property name="username" value="${mail.username}"/>
    <property name="password" value="${mail.password}"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
            <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
        </props>
    </property>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="velocityEngine"
      class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <value>
            resource.loader=class
               class.resource.loader.class=
   org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
        </value>
    </property>
</bean>

<bean id="portletMultipartResolver"
      class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>
</beans>

web.xml:

<web-app id="WebApp_9" 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">

<display-name>Portlet MVC</display-name>

<servlet>
    <servlet-name>ViewRendererServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
</servlet>

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

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<servlet-mapping>
    <servlet-name>ViewRendererServlet</servlet-name>
    <url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/**/applicationContext.xml</param-value>
</context-param>

推荐答案

问题与您的web.xml文件有关.您需要做的是添加以下侦听器:

The problem is related to your web.xml file. What you need to do is add the following listener:

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

请注意,Liferay 6.1和自定义spring-mvc portlet部署( LPS-29103 )存在一个错误.如果您使用Liferay自动部署过程,则会出现此问题,此修复程序已经发布.由于自动部署过程会修改web.xml文件并将侦听器按不正确的顺序放置,因此导致出现此问题.

Note there is a bug with Liferay 6.1 and custom spring-mvc portlet deployments (LPS-29103). The problem occurs if you use the Liferay auto-deploy process, a fix has already been released. The issue results because the auto-deploy process amends the web.xml file and sticks the listener in the incorrect order.

您需要做的是在spring上下文侦听器之后添加侦听器(SecurePluginContextListener).因此,在您的情况下,它将是:

What you need to do is add the listener (SecurePluginContextListener) after your spring context listener. So in your case it would be:

<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>

希望这可以解决您的问题.

Hope this resolves your issue.

这篇关于Liferay + Spring + Spring Web Mvc-@Autowired不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:44