spel表达式的用法_el表达式判断是否为空

spel表达式的用法_el表达式判断是否为空spel表达式运算使用翻看公司代码,这一块不是很懂,查资料,记一下,还是太菜1.常用的对象Expression:表达式对象SpelExpressionParser:表达式解析器EvaluationContext:上下文2.使用本文参考了下面的几篇文章https://www.cnblogs.com/shihuc/p/9338173.htmlhttps://blog.csdn.net/f641385712/article/details/90812967下面的例子主要是来源于第一

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

spel表达式运算使用

翻看公司代码,这一块不是很懂,查资料,记一下,还是太菜

1. 常用的对象

  • Expression: 表达式对象
  • SpelExpressionParser:表达式解析器
  • EvaluationContext:上下文

2. 使用

本文参考了下面的几篇文章

public class AccessingPropertiesApplication { 
   
    static Logger logger = Logger.getLogger(AccessingPropertiesApplication.class);
    public static void main(String []args) { 
   
        ExpressionParser parser = new SpelExpressionParser();
        //a. 调用String这个javaBean的'getBytes()'
        Expression exp = parser.parseExpression("'Hello World'.bytes");
        byte[] bytes = (byte[]) exp.getValue();
        logger.info("字节内容:"+ bytes);

        //b.嵌套的bean实例方法调用,通过'.'运算符
        exp = parser.parseExpression("'Hello World'.bytes.length");
        int len = (Integer) exp.getValue();
        logger.info("字节长度:" + len);

        //c. property訪問
        GregorianCalendar c = new GregorianCalendar();
        c.set(1856, 7, 9);
        //Inventor的构造函数参数分别是:name, birthday, and nationality.
        Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
        parser = new SpelExpressionParser();
        exp = parser.parseExpression("name");
        EvaluationContext context = new StandardEvaluationContext(tesla);
        String name = (String) exp.getValue(context);
        logger.info("Inventor: " + name);

        //对对象实例的成员进行操作。 evals to 1856, 注意纪年中,起点是从1900开始。
        int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
        //Inventor tesla设置出生地(瞎写的信息)。
        tesla.setPlaceOfBirth(new PlaceOfBirth("America city", "America"));
        String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
        logger.info("year: " + year + ", city: " + city);

        //d. array, list操作
        // 先测试验证array
        tesla.setInventions(new String []{ 
   "交流点","交流电发电机","交流电变压器","变形记里面的缩小器"});
        EvaluationContext teslaContext = new StandardEvaluationContext(tesla);
        String invention = parser.parseExpression("inventions[3]").getValue(teslaContext, String.class);
        logger.info("Array: " + invention);
        //list测试验证
        Society society = new Society();
        society.addMember(tesla);
        StandardEvaluationContext societyContext = new StandardEvaluationContext(society);
        // evaluates to "Nikola Tesla"
        String mName = parser.parseExpression("Members[0].Name").getValue(societyContext, String.class);
        // List and Array navigation
        // evaluates to "Wireless communication"
        String mInvention = parser.parseExpression("Members[0].Inventions[2]").getValue(societyContext, String.class);
        logger.info("List: mName= " + mName + ", mInvention= " + mInvention);

        //e. Map的操作
        //首先构建数据环境
        GregorianCalendar cm = new GregorianCalendar();
        cm.set(1806, 7, 9);
        Inventor idv = new Inventor("Idovr", cm.getTime(), "China,haha");
        Society soc = new Society();
        idv.setPlaceOfBirth(new PlaceOfBirth("Wuhan","China"));
        soc.addOfficer(Advisors, idv);
        soc.addOfficer(President, tesla);
        EvaluationContext socCtxt = new StandardEvaluationContext(soc);
        Inventor pupin = parser.parseExpression("Officers['president']").getValue(socCtxt, Inventor.class);
        String mCity = parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(socCtxt, String.class);
        logger.info("Map case 1: " + mCity);
        // setting values
        Expression mExp = parser.parseExpression("Officers['advisors'].PlaceOfBirth.Country");
        mExp.setValue(socCtxt, "Croatia");
        //下面注释掉的,是官方的做法,这个是有问题的,基于我的研究环境
        //parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(socCtxt, "Croatia");
        //String country = parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").getValue(socCtxt, String.class);
        String country = mExp.getValue(socCtxt, String.class);
        logger.info("Map case 2: " + country);
    }
}

3. 我自己的例子

  • 实体类
package com;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Data
public class A{ 
   
    private Integer id ;
    private String name ;
    private String ccc ;
    List<String> lists = new ArrayList<>(10);

    public A(Integer id, String name, String ccc) { 
   
        this.id = id;
        this.name = name;
        this.ccc = ccc;
        IntStream.range(0,10).forEach(t->lists.add(String.valueOf(t)));
    }
    @Override
    public String toString() { 
   
        return "A{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", list=" + ccc +
                '}';
    }
}
  • 校验类
   Expression  exp = null;
        SpelExpressionParser parser = null;
        A tesla = new A(1, "aaa","ccc");
        parser = new SpelExpressionParser();
        //属性名.方法或者属性
        exp = parser.parseExpression("lists.size() > 0");
        EvaluationContext context = new StandardEvaluationContext(tesla);
        /** * 这个 context 是上下文。 * getValue(第一个是 context ,第二个是想要返回的值)这个值是可要可不要的. 不指定类型的话返回的就是 object了 */
        Boolean value = exp.getValue(context, Boolean.class);
        int year = (Integer) parser.parseExpression("id + 1900").getValue(context);
        logger.info("year,{}",year);
        logger.info("Inventor: " + value);

记一次错误

  • 报错信息为:
    maybe not public or not valid
  • 解决方式
    添加get和set方法
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197325.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 二叉树的建立及其递归遍历(C语言实现)

    二叉树的建立及其递归遍历(C语言实现)最近在学习数据结构中树的概念,迟迟不得入门,应该是自己的懒惰和没有勤加练习导致的,以后应该多加练习以下是我对二叉树的一些总结内容二叉树的特点有:-每一个节点最多有两棵子树,所以二叉树中不存在度大于2的节点,注意,是最多有两棵,没有也是可以的左子树和右子树是有顺序的,次序不能颠倒,这点可以在哈夫曼编码中体现,顺序不同编码方式不同-即使树中某个节点中只有一个子树的花,也要区分它…

    2022年4月28日
    81
  • 用java输出语句_Java的常用输入输出语句

    用java输出语句_Java的常用输入输出语句一、概述输入输出可以说是计算机的基本功能。作为一种语言体系,java中主要按照流(stream)的模式来实现。其中数据的流向是按照计算机的方向确定的,流入计算机的数据流叫做输入流(inputStream),由计算机发出的数据流叫做输出流(outputStream)。Java语言体系中,对数据流的主要操作都封装在java.io包中,通过java.io包中的类可以实现计算机对数据的输入、输出操作。在编…

    2022年7月7日
    21
  • jQuery模拟打字逐字输出代码

    效果查看:http://hovertree.com/texiao/jquery/70/jQuery键盘打出逐字逐句显示特效,逐字逐句显示文字还可以设置每个文字随机颜色:http://hovert

    2021年12月24日
    41
  • js中settimeout()的用法详解_低噪放工作原理

    js中settimeout()的用法详解_低噪放工作原理基本原理setTimeout(func,delay,args):设置超时调用,经过delay时间后,将func函数加入到执行队列中准备调用。如果队列为空,立即执行该函数,否则等待线程空闲再执行。setInterval(func,interval,args):设置…

    2022年10月4日
    2
  • php sigpipe,遭遇SIGPIPE[转]

    php sigpipe,遭遇SIGPIPE[转]转自:http://www.diybl.com/course/3_program/c++/cppjs/20090831/173152.html我写了一个服务器程序,在Windows下在cygwin环境编译后执行,然后用C#写了多线程客户端进行压力测试.程序一直运行正常.但当在Linux下测试时,总是莫名退出.最后跟踪到是write调用导致退出.用gdb执行程序,退出时提示”Broken…

    2022年5月30日
    36
  • 如何生成lib文件_bin文件和mcs文件

    如何生成lib文件_bin文件和mcs文件看到一篇文章可以添加crc文章链接:http://blog.csdn.net/Simon223/article/details/105724950感觉应该可以可以添加一些自己的其他信息。先做个标记吧,后面再来学keil官网的摘抄信息:https://www.keil.com/support/docs/3806.htmµVISION:CRCExampleInformationinthisknowledgebasearticleappliesto:MDK-ARM5.00o

    2022年10月20日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号