legacy-knowledge-base
公開されました Jul. 2, 2025

Springを使ったコアクラスの拡張とExtプラグインの併用

written-by

Liferay Support

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

legacy-article

learn-legacy-article-disclaimer-text

この記事は、Liferay DXPでExtプラグインを使用するためのサポートされたユースケースを文書化したもので、Liferay Digital Enterpriseのコアクラス(例: portal-impl, portal-kernel, etc)をSpringを使って拡張できるようにします。 コアクラスを区別するために、 Finding Core Liferay Portal Artifacts のセクションを参照することができます。 Extプラグインを作成する前に、一般的な Customization with Ext Plugins チュートリアルを確認したことを確認してください。

解決策

例として、 portal-impl.jarに存在する PortalImpl コアクラスを継承するサンプル Ext プラグインを作成します。 サーバーのノード名を返す PortalImpl.getComputerName() メソッドを Spring Bean 経由でオーバーライドすることになります。 Extプラグインはこのメソッドをオーバーライドして、サーバーから返されるノード名を変更します。

  1. Plugins SDK の /ext フォルダに移動して、以下のコマンドを実行します:

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

    Extプラグインが生成され、Plugins SDKの /ext フォルダに、割り当てた名前の後に -ext が付いた状態で存在します(例: portal-impl-extend-spring-ext)。

  2. Liferay DXPのインストールでサーバーのノード名を表示するのは、デフォルトでは false に設定されています。 このプロパティを有効にする必要があります。 これを行うには、Liferayバンドルのルートフォルダに移動して、 portal-ext.properties ファイルを作成します。 そのファイルに、以下のプロパティを挿入します:

    web.server.display.node=true
    

    これで、Liferayバンドルを再起動すると、サーバーのノード名が表示されるようになりました。

  3. /ext-impl/src フォルダに、新しいクラスを配置したいパッケージ名を表すフォルダ構造を作成します(例: com/liferay/portal/util)。 次に、新しいJavaクラスを作成します:

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

拡張クラスで定義されたメソッドは、 PortalImpl.getComputerName() メソッドをオーバーライドします。 "SAMPLE_EXT_INSTALLED_" Stringの前に、サーバーのノード名を付けるようにしました。

  1. Extプラグインの /ext-impl/src フォルダに、 META-INF/ext-spring.xml ファイルを作成します。 このファイルに、次のコードを挿入します:

    <?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>

コアサービスクラスを修正する予定なので、Spring beanを介してその拡張クラスを注入することができます。 これにより、新しいクラスが確実に認識されるようになります。 拡張クラスの完全定義クラス名(例: com.liferay.portal.util.SamplePortalImpl)をBeanタグの class 属性に、完全定義オリジナルクラス名(例: com.liferay.portal.util.PortalImpl)をBeanタグの id 属性に割り当ててください。

Extプラグインがデプロイされると、新しいサービス(例えば、 SamplePortalImpl)は、コア PortalImpl クラスを拡張します。

Liferay Digital Enterpriseのコアクラスを拡張するExtプラグインを作成しました! Deploy the Plugin セクションの説明に従って、サーバーにプラグインを導入してください。

did-this-article-resolve-your-issue

legacy-knowledge-base