Wednesday, January 7, 2015

Using Spring in Liferay portlet

In src/main/resources folder, create META-INF folder, inside this folder, create ext-spring.xml file. This file contains bean definition, like below:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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/oxm
        http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        ">
       
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
           <list>
               <value>/WEB-INF/config/props.properties</value>
            </list>
        </property>
    </bean>
   
    <!-- Some bean -->
    <bean id="myBean" class="com.example.MyBean" /> 
</beans>


Also, in src/main/resources folder, create service.properties file:

##
## Properties Override
##

    #
    # Specify where to get the overridden properties. Updates should not be made
    # on this file but on the overridden version of this file.
    #
    include-and-override=${base.path}/service-ext.properties


##
## Spring
##

    #
    # Input a list of comma delimited Spring configurations. These will be
    # loaded after the bean definitions specified in the
    # portalContextConfigLocation parameter in web.xml.
    #
    spring.configs=\
        WEB-INF/classes/META-INF/base-spring.xml,\
        \
        WEB-INF/classes/META-INF/hibernate-spring.xml,\
        WEB-INF/classes/META-INF/infrastructure-spring.xml,\
        \
        WEB-INF/classes/META-INF/cluster-spring.xml,\
        \
        WEB-INF/classes/META-INF/portlet-spring.xml,\
        \
        WEB-INF/classes/META-INF/dynamic-data-source-spring.xml,\
        WEB-INF/classes/META-INF/shard-data-source-spring.xml,\
        \
        WEB-INF/classes/META-INF/ext-spring.xml


Now, your bean will loaded every time Liferay started.

In your *LocalServiceUtil, if you would like to use the bean in ext-spring.xml file, use this code:

public class YourServiceUtil {
     public static void someFunc() {
        getService().someFunc();
    }
   
    public static void clearService() {
        _service = null;
    }

    public static YourServiceImpl getService() {
        if (_service == null) {
           
_service = (YourServiceImpl ) PortletBeanLocatorUtil
                    .locate(ClpSerializer.getServletContextName(), "your_bean_name");
        }

        return _service;
    }

    private static YourServiceImpl _service;
}


ClpSerializer.getServletContextName() is a class return the contextName of the context where bean located. Usually, it is your project name.

No comments:

Post a Comment