« 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 添付ファイル
    34801 参照数
    平均 (0 投票)
    平均評価は0.0星中の5です。
    コメント
    コメント 作成者 日時
    Thank you for this sample! One minor thing:... Hubert Felber 2010/01/28 6:42
    Is this still the way to do it for v5.2.3? Pablo Krause 2010/02/26 12:48
    Hello all, I followed the above article step... Heba El Ayoty 2011/01/24 1:01
    Hey, did you solve it in Liferay 6.0.5? Viktor Palai 2011/03/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/01/28 6:42
    Is this still the way to do it for v5.2.3?
    Hubert Felberへのコメント。投稿日時:10/02/26 12:48
    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/01/24 1:01
    Hey,

    did you solve it in Liferay 6.0.5?
    Heba El Ayotyへのコメント。投稿日時:11/03/25 4:56