首先有几个概念需要明确。
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

Liferay7 BPM门户开发之25: Liferay7应用程序配置(APPLICATION CONFIGURATION)的更多相关文章

  1. Liferay7 BPM门户开发之37: Liferay7下的OSGi Hook集成开发

    hook开发是Liferay客制扩展的一种方式,比插件灵活,即可以扩展liferay门户,也能对原有特性进行更改,Liferay有许多内置的服务,比如用hook甚至可以覆盖Liferay服务. 可作为 ...

  2. Liferay7 BPM门户开发之34: liferay7对外服务类生成(RestService Get Url)

    在liferay7中开发不依赖Service Builder的对外服务类,非常简洁,只需要2点注解: 在类的前部定义: @ApplicationPath("/PathXXX") 方 ...

  3. Liferay7 BPM门户开发之22: Liferay7模型监听器(Model Listeners)

    Model Listeners实现ModelListener接口,用于持久化时的AOP处理 一些使用场景: Audit Listener: 在一个独立而分离的数据库,做信息更新的审计: Cache C ...

  4. Liferay7 BPM门户开发之24: Liferay7应用程序安全

    整理中...... Resources, Roles, and PermissionsPortal Access Control List (PACL) Custom SSO Providers Au ...

  5. Liferay7 BPM门户开发之1:Liferay7开发环境准备

    liferay sdk下载 \IDE下载 \ Tomcat 安装细节不在此赘述 网上有很多. 只讲核心关键坑点 进入2016年,从Liferay6.2.5 ga6版本开始,到7.0 ga3,在ivy环 ...

  6. Liferay7 BPM门户开发之17: Portlet 生命周期

    Portlet 生命周期 init() =〉 render() =〉 processAction() =〉 processEvent() =〉 serveResource() =〉destroy() ...

  7. Liferay7 BPM门户开发之10: 通用流程实现从Servlet到Portlet(Part1)

    开发目的: 实现通用流程自动化处理(即实现不需要hardcode代码的bpm统一处理后台,仅需要写少量前端html form代码和拖拽设计BPM定义) 既可独立运行或可依托于Liferay或依托其它门 ...

  8. Liferay7 BPM门户开发之12:acitiviti和liferay用户权限体系集成

    写到第12章才出现Liferay的内容,希望可以厚积薄发. 我们的目标是不使用不维护Activiti的用户组织架构,只维护Liferay的体系,这样的好处是非常明显的,即不用做组织架构的同步工作. 原 ...

  9. Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)

    第一步:修改liferay-hook.xml <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Lifer ...

随机推荐

  1. linux grep (linux查找关键字在php出现的次数)

    http://www.th7.cn/system/lin/201508/127681.shtml 查找CleverCode在当前目录以及子目录,所有的php出现大于0的次数. # find -type ...

  2. SpringCloud Feign context-path踩到的坑

    最近在使用SpringCloud的context-path时,遇到了一些坑,记录一下. server.context-path(上下文) 服务提供者的application配置文件中有一个属性叫ser ...

  3. POJ-3268.SilverCowParty.(最短路 + 图的转置)

    本题思路:对原图和原图的逆图分别用一次最短路,找出最大值即可. 一开始是我是对每个顶点spfa搜了一波,结果判题时间巨长,还好这个题的数据量不是很大,所以就用了另一种思路. 参考代码:spfa单结点爆 ...

  4. 37 【kubernetes】搭建dashboard

    官方的操作步骤是:https://github.com/kubernetes/dashboard 我自己的步骤是: 1,下载yaml文件 wget https://raw.githubusercont ...

  5. ES6的let和const的不同点

    详细的异同点请点击:https://blog.csdn.net/zhouziyu2011/article/details/71366078

  6. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. Spring Kafka中关于Kafka的配置参数

    #################consumer的配置参数(开始)################# #如果'enable.auto.commit'为true,则消费者偏移自动提交给Kafka的频率 ...

  8. java 远程debug

    在启动jar包添加如下参数16091是端口 java -Xdebug -Xrunjdwp:transport=dt_socket,address=16091,server=y,suspend=n -j ...

  9. PHP开发——变量

    变量的概念 l  变量是临时存储数据的容器: l  变量是存储内存当中: l  我们现实中有很多数据:姓名.性别.年龄.学历等: l  在计算机中,用变量来代替一个一个的数据: l  我们可以把计算机 ...

  10. redis安全删key脚本(模糊匹配,长list,大set等)

    两种情况: 1.删除指定前缀开头的rediskey ,扫描和删除过程中对线上无感知 2.删除一个大的list,set,zset,hash,这种得分批次减少大小,一直缩到0再删 第一种情况:只要知道线上 ...