Extending Core Classes Using Spring with Ext Plugins

This article documents a supported use case for using Ext plugins in Liferay DXP, which enables you to extend Liferay Digital Enterprise's core classes (e.g., portal-implportal-kernel, etc.) using Spring. You can reference the Finding Core Liferay Portal Artifacts section for help distinguishing core classes. Make sure you have reviewed the generalized Customization with Ext Plugins tutorial before creating an Ext plugin.

Resolution

As an example, you'll create a sample Ext plugin that extends the PortalImpl core class residing in the portal-impl.jar. You'll override the PortalImpl.getComputerName() method via Spring bean, which returns your server's node name. The Ext plugin will override this method and modify the server's returned node name.

  1. Navigate into your Plugins SDK's /ext folder and run the following command:

    create.[bat|sh]  portal-impl-extend-spring "PortalImpl Extend Spring"
    

    Your Ext plugin is generated and now resides in the Plugins SDK's /ext folder with the name you assigned followed by -ext (e.g., portal-impl-extend-spring-ext).

  2. Displaying the server node name in your Liferay DXP installation is set to false by default. You'll need to enable this property. To do this, navigate into your Liferay bundle's root folder and create a portal-ext.properties file. In that file, insert the following property:

    web.server.display.node=true
    

    Now your server's node name will be displayed once your Liferay bundle is restarted.

  3. In the /ext-impl/src folder, create the folder structure representing the package name you want your new class to reside in (e.g., com/liferay/portal/util). Then create your new Java class:

    package com.liferay.portal.util;
    
    public class SamplePortalImpl extends PortalImpl {
    @Override public String getComputerName() { return "SAMPLE_EXT_INSTALLED_" + super.getComputerName(); } 
    }

The method defined in the extension class overrides the PortalImpl.getComputerName() method. The "SAMPLE_EXT_INSTALLED_" String is now prefixed to your server's node name.

  1. In your Ext plugin's /ext-impl/src folder, create a META-INF/ext-spring.xml file. In this file, insert the following code:

    <?xml version="1.0"?>
    
    <beans
        default-destroy-method="destroy"
        default-init-method="afterPropertiesSet"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
    >
    &lt;bean class="com.liferay.portal.util.SamplePortalImpl" id="com.liferay.portal.util.PortalImpl" /&gt; 
    </beans>

Since you plan on modifying a core service class, you can inject its extension class via a Spring bean. This will ensure your new class is recognized. Assign your extension class's fully defined class name (e.g., com.liferay.portal.util.SamplePortalImpl) to the bean tag's class attribute and the fully defined original class name (e.g., com.liferay.portal.util.PortalImpl) to the bean tag's id attribute.

When your Ext plugin is deployed, your new service (e.g., SamplePortalImpl) will extend the core PortalImpl class.

Awesome! You've created an Ext plugin that extends a core class in Liferay Digital Enterprise! Follow the instructions in the Deploy the Plugin section to deploy it to your server.

Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0