« 返回到 Extension...

Business Logic Customization Example

Table of Contents [-]

    So you love everything about a built in Liferay function except for one thing, what should you do? Customize it of course! But how does one change the business logic in a Liferay Impl class without changing the code? This is what this document will show you.

    Let's take adding a new User for example. By default, you are required to enter a screen name, valid email address, first name, and last name. Let's try to change the business logic so that screen name is NOT required.

    The Action Class / method controlling the form submit is EditUserAction.updateUser(). Somewhere in there UserServiceUtil.addUser() is called, and we know that Liferay Util classes are autogenerated via Service Builder. The business logic lies within UserLocalServiceImpl.addUser().

    Steps:

    1. Create a new class that extends UserLocalServiceImpl.java and include the methods you want to override. Place this class in your ext environment. For example:

      package com.ext.portal.service.impl;
      public class ExtUserLocalServiceImpl extends UserLocalServiceImpl {
         public User addUser(
           ....;
           ....;
         )
         protected void validate(
           ....;
           ....;
         )
      }

    2. Modify spring configuration file to tell the Util to talk to the Impl. An easy way to do this is to search for "UserLocalServiceImpl" in portal-spring.xml. You will see this:

     <bean id="com.liferay.portal.service.UserLocalService.professional" class="com.liferay.portal.service.impl.UserLocalServiceImpl" lazy-init="true" />

    We don't to touch portal-spring.xml or ext-spring.xml because their contents are autogenerated. So we'll create a new spring configuration file called custom-spring-info.xml and place it in ext-impl/classes/META-INF/. Input your new Impl reference:

     <bean id="com.liferay.portal.service.UserLocalService.professional" class="com.ext.portal.service.impl.ExtUserLocalServiceImpl" lazy-init="true" />

    Note: for some reason the name custom-spring.xml does not work. custom-spring-info.xml does work.

    3. Modify portal-ext.properties to read your new spring configuration file:

        spring.configs=\
            META-INF/activemq-spring-jms.xml,\
            META-INF/data-source-spring.xml,\
            META-INF/misc-spring.xml,\
            META-INF/counter-spring.xml,\
            META-INF/documentlibrary-spring.xml,\
            META-INF/documentlibrary-spring-jms.xml,\
            META-INF/lock-spring.xml,\
            META-INF/mail-spring.xml,\
            META-INF/mail-spring-jms.xml,\
            META-INF/portal-spring.xml,\
            META-INF/portal-spring-jcr.xml,\
            META-INF/portal-spring-jms.xml,\
            META-INF/ext-spring.xml,\
            META-INF/custom-spring-info.xml
            

    4. Deploy your changes to tomcat and your good to go. Place a debug point in your custom class to make sure it's getting read.

    0 附件
    34803 查看
    平均 (0 票)
    满分为 5,平均得分为 0.0。
    评论
    讨论主题回复 作者 日期
    Thank you for this sample! One minor thing:... Hubert Felber 2010年1月28日 上午6:42
    Is this still the way to do it for v5.2.3? Pablo Krause 2010年2月26日 下午12:48
    Hello all, I followed the above article step... Heba El Ayoty 2011年1月24日 上午1:01
    Hey, did you solve it in Liferay 6.0.5? Viktor Palai 2011年3月25日 上午4:56

    Thank you for this sample!
    One minor thing: custom-spring-info.xml should go to src\META-INF not classes\META-INF. Otherwise "ant clean" will remove it.
    在 10-1-28 上午6:42 发帖。
    Is this still the way to do it for v5.2.3?
    在 10-2-26 下午12:48 发帖以回复 Hubert Felber
    Hello all,
    I followed the above article step by step in Liferay 6.0.5 Enterprise edition, but it doesn't work with me....
    no errors but it seems like liferay doesn't feel that anything new is happened emoticon
    在 11-1-24 上午1:01 发帖。
    Hey,

    did you solve it in Liferay 6.0.5?
    在 11-3-25 上午4:56 发帖以回复 Heba El Ayoty