Friday, August 9, 2013

How to use Spring in a Liferay Portlet

This post shows how add Spring into a Liferay portlet.
In certain cases, we might need to have IoC in our existing application. eg, lookup configuration from xml.
adding Spring in our application might ease the xml lookup.

Steps:
1. add Spring jar from Liferay classpath
    a) add the following Spring libraries into an existing Liferay portlet project from Liferay Portal
    spring-aop.jar,\
    spring-asm.jar,\
    spring-beans.jar,\
    spring-context-support.jar,\
    spring-context.jar,\
    spring-core.jar,\
    spring-expression.jar,\
    spring-web.jar
    refer this post to know how to add Liferay Portal libraries into a portlet.

2. Once Spring libraries added, the next step is to add Spring listener.
    a) add the following listener into web.xml
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3. Ready to use
    a) create the required pojo
    package test;
    public class MyBean {
        String name;
        int age;
        String course;
        // getter & setter
    }

    b) create the Spring xml for IoC lookup in META-INF folder
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<bean id="MY_TEST_BEAN" class="test.MyBean">
    <property name="name"   value="kian" />
    <property name="age"    value="23" />
    <property name="course" value="Java Programming" />
</bean>
</beans>

    c) Add the Spring xml into web application context by adding it into web.xml
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>META-INF/mySpringTest.xml</param-value>
    </context-param>

    d) lookup the Spring bean with the following codesnnnn
public void getContextBean() {
    // getting ServletContext
    PortletRequest pr = (PortletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
    HttpServletRequest request = PortalUtil.getHttpServletRequest(pr);
    ServletContext servletContext = request.getSession().getServletContext();

    // getting WebApplicationContext
    webApplicationContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);

    // getting settings as object
    MyBean bean = (MyBean)webApplicationContext.getBean("MY_TEST_BEAN");

    System.out.println("name: " + bean.getName());
    System.out.println("age: " + bean.getAge());
    System.out.println("course: " + bean.getCourse());
}

Result:
name: kian
age: 23
course: Java Programming


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...