Liferay7 BPM门户开发之25: Liferay7应用程序配置(APPLICATION CONFIGURATION)

本文介绍了Liferay7中应用程序配置的三个核心概念:类型化配置、模块性和不同范围内的配置能力,并通过具体示例展示了如何实现这些配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


首先有几个概念需要明确。
1、第一个概念是这里的应用程序配置不是写XML之类的配置文件,是类似字典的类型化配置
这意味着应用程序配置不只是一个字符串键值对的列表。值还可以有类型,如整数列表,字符串列表,一个网址,甚至可以使用自己的自定义类型。

2、第二个概念是模块性。在Liferay 7 中,应用程序是模块化的,组件只是一个有@组件注释的类,通常是一组属性提供元数据。

3、第三个概念是在不同的范围内具有相同的应用程序的能力有不同的配置。如果您的应用程序需要在不同的范围支持不同的配置,应用程序作用范围可以有:

  • System: 所有的应用程序范围;
  • Virtual Instance: 虚拟实例级别;
  • Site: 站点级别,即每个站点不同的配置;
  • Portlet Instance: Portlet实例级别,即每个Portlet实例都有不同配置;


实例

编写应用程序配置接口

package com.liferay.docs.exampleconfig;

import aQute.bnd.annotation.metatype.Meta;

@Meta.OCD(id = "com.liferay.docs.exampleconfig.ExampleConfiguration")
public interface ExampleConfiguration {

    @Meta.AD(
        deflt = "blue",
        required = false
    )
    public String favoriteColor();

    @Meta.AD(
       deflt = "red|green|blue",
       required = false
    )
    public String[] validColors();

    @Meta.AD(required = false)
    public int favoriteNumber();

}
Meta.AD 和 Meta.OCD是OSGi Metatype 的格式。更多信息见:
http://bnd.bndtools.org/chapters/210-metatype.html


MVCPortlet的实现
先添加@Component注解, configurationPid即应用程序配置接口
@Activate和@Modified是一个标准格式写法

package com.liferay.docs.exampleconfig;

import java.io.IOException;
import java.util.Map;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;

import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import aQute.bnd.annotation.metatype.Configurable;

@Component(
    configurationPid = "com.liferay.docs.exampleconfig.ExampleConfiguration",
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.security-role-ref=power-user,user",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.resource-bundle=content.Language"
    },
    service = Portlet.class
)
public class ExampleConfigPortlet extends MVCPortlet {

    @Override
    public void doView(RenderRequest renderRequest,
        RenderResponse renderResponse) throws IOException, PortletException {
        //设置renderRequest的属性,把className写到key
        renderRequest.setAttribute(
            ExampleConfiguration.class.getName(), _configuration);
        
        super.doView(renderRequest, renderResponse);
    }

    
    public String getFavoriteColor(Map colors) {
        return (String) colors.get(_configuration.favoriteColor());
    }

    @Activate
    @Modified
    protected void activate(Map<String, Object> properties) {
        _configuration = Configurable.createConfigurable(
        ExampleConfiguration.class, properties);
    }    

    private volatile ExampleConfiguration _configuration;

}

 

jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ page import="com.liferay.docs.exampleconfig.ExampleConfiguration" %>

<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>

<portlet:defineObjects />

<liferay-theme:defineObjects />

<p>
<b>Hello from the Example Configuration portlet!</b>
</p>

<%
ExampleConfiguration configuration = (ExampleConfiguration) GetterUtil.getObject(
renderRequest.getAttribute(ExampleConfiguration.class.getName()));

String favoriteColor = configuration.favoriteColor();
%>

<p>Favorite color: <span style="color: <%= favoriteColor %>;"><%= favoriteColor %></span></p>

 

界面:


我暂时还想不出这种写法的好处是啥?

为啥不用枚举,静态类,或者XML的属性文件,就是为了有作用范围? 有知道答案的客官请点醒我。
这种注解+元数据+接口的写法实在是太奇特了。

在liferay自身的设置,也是应用这种配置方法,

比如UI设置,如图:


还有一个更复杂的例子:
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/implementing-configuration-actions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值