Search code examples
javaspringautowiredliferay-7

Spring autowired annotation in liferay 7.2


Hi everyone

I want to use spring-core in liferay 7.2 with @Autowired annotation. So, i have next 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());
    }
}

So, what i must to do in web.xml or in portlet.xml to set context listener of spring-core ? For example in web-servlet application i must to add

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

Solution

  • Actually problem was in spring version

    I used 3.2.0 which doesnt support java 8. When i switched to 4.0.0 everything become ok.

    In conclusion. For usage spring core in portlet project for liferay i must:

    • Add context listener into my WEB-INF/web.xml file
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value> 
    </context-param>
    
    • Then in every portlet use following method to init @Autowired field
    @Override
    public void init() throws PortletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
    
    • I have following persistence unit
    <persistence-unit name="departments">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.aimprosoft.dao.model.Employee</class>
        <class>com.aimprosoft.dao.model.Department</class>
        <properties>
            <property name="hibernate.connection.username" value="username"/>
            <property name="hibernate.connection.password" value="password"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/departments"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        </properties>
    </persistence-unit>
    
    • Finally spring context looks following
    <context:annotation-config/>
    <context:component-scan base-package="com.aimprosoft"/>
    <bean id="departments" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="WEB-INF/persistence.xml"/>
    </bean>
    <bean id="textResource" class="java.util.ResourceBundle" factory-method="getBundle">
        <constructor-arg value="text_bundle"/>
    </bean>
    <tx:annotation-driven />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>