liferay 7.2 中的 Spring 自动装配注释

我想在带有注释的 liferay 7.2 中使用 spring-core @Autowired。所以,我有下一个 portlet


package com.aimprosoft.module;


import com.aimprosoft.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;


import javax.portlet.GenericPortlet;

import javax.portlet.PortletException;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

import java.io.IOException;


public class SamplePortlet  extends GenericPortlet {


    @Autowired

    private EmployeeService employeeService;


    @Override

    protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {

        response.getWriter().println(employeeService.toString());

    }

}

那么,我必须在 web.xml 或 portlet.xml 中做什么来设置 spring-core 的上下文侦听器?例如在 web-servlet 应用程序中我必须添加


    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>


九州编程
浏览 79回答 1
1回答

江户川乱折腾

实际上问题出在春季版本我使用的是不支持 java 8 的 3.2.0。当我切换到 4.0.0 时,一切都正常了。综上所述。为了在 liferay 的 portlet 项目中使用 spring core,我必须:将上下文监听器添加到我的 WEB-INF/web.xml 文件中<listener>&nbsp; &nbsp; <listener-class>&nbsp; &nbsp; &nbsp; &nbsp; org.springframework.web.context.ContextLoaderListener&nbsp; &nbsp; </listener-class></listener><context-param>&nbsp; &nbsp; <param-name>contextConfigLocation</param-name>&nbsp; &nbsp; <param-value>WEB-INF/applicationContext.xml</param-value>&nbsp;</context-param>然后在每个 portlet 中使用以下方法来初始化@Autowired字段@Overridepublic void init() throws PortletException {&nbsp; &nbsp; SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);}我有以下持久性单元<persistence-unit name="departments">&nbsp; &nbsp; <provider>org.hibernate.ejb.HibernatePersistence</provider>&nbsp; &nbsp; <class>com.aimprosoft.dao.model.Employee</class>&nbsp; &nbsp; <class>com.aimprosoft.dao.model.Department</class>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <property name="hibernate.connection.username" value="username"/>&nbsp; &nbsp; &nbsp; &nbsp; <property name="hibernate.connection.password" value="password"/>&nbsp; &nbsp; &nbsp; &nbsp; <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>&nbsp; &nbsp; &nbsp; &nbsp; <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/departments"/>&nbsp; &nbsp; &nbsp; &nbsp; <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>&nbsp; &nbsp; </properties></persistence-unit>最后 spring 上下文看起来如下<context:annotation-config/><context:component-scan base-package="com.aimprosoft"/><bean id="departments" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">&nbsp; &nbsp; <property name="persistenceXmlLocation" value="WEB-INF/persistence.xml"/></bean><bean id="textResource" class="java.util.ResourceBundle" factory-method="getBundle">&nbsp; &nbsp; <constructor-arg value="text_bundle"/></bean><tx:annotation-driven /><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java