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

本文主要是介绍Liferay7 BPM门户开发之25: Liferay7应用程序配置(APPLICATION CONFIGURATION),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


首先有几个概念需要明确。
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 {@Overridepublic 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@Modifiedprotected 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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/666028

相关文章

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

VSCode开发中有哪些好用的插件和快捷键

《VSCode开发中有哪些好用的插件和快捷键》作为全球最受欢迎的编程工具,VSCode的快捷键体系是提升开发效率的核心密码,:本文主要介绍VSCode开发中有哪些好用的插件和快捷键的相关资料,文中... 目录前言1、vscode插件1.1 Live-server1.2 Auto Rename Tag1.3

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大