• Spring SpEL表达式语言


    11.2 SpEL 支持如下表达式

    • 基本表达式:字面量表达式、关系、逻辑与算术运算表达式、字符串连接及截取表达式、三目运算表达式、正则表达式、括号优先级表达式;

    • 类相关表达式:类类型表达式、类实例化、instanceof 表达式、变量定义及引用、赋值表达式、自定义函数、对象属性存取及安全导航表达式、对象方法调用、Bean 引用;

    • 集合相关表达式:内联 List、内联数组、集合、字典访问、列表、字典、数组修改、集合投影、集合选择;不支持多维内联数组初始化;不支持内联字典定义;

    • 其他表达式:模板表达式。

    12.3 使用Spel

    @Test
    public void testDemo03(){
        //使用 SpEL 输出一个简单的字符串
        SpelExpressionParser spel = new SpelExpressionParser();
        Expression exp = spel.parseExpression("'Hello,world!'");
        String message = (String) exp.getValue();
        System.out.println(message);
        //使用 SpEL 调用 String 的属性 bytes,将字符串转换为字节数组。
        exp = spel.parseExpression("'Hello,world!'.bytes");
        byte[] bytes = (byte[]) exp.getValue();
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i]+" ");
        }
        System.out.println();
        //SpEL 还支持使用嵌套属性,下面将字符串转换为字节后获取长度,代码如下。
        exp = spel.parseExpression("'Hello,world!'.bytes.length");
        int num = (int) exp.getValue();
        System.out.println(num);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    @Test
    public void testDemo04(){
        //使用 SpEL还可以调用方法、访问属性和调用构造函数。
        SpelExpressionParser spel = new SpelExpressionParser();
        Expression exp = spel.parseExpression("'Hello,world'.concat('!')");
        String message = (String) exp.getValue();
        System.out.println(message);
        //字符串的构造函数可以被调用,而不是使用字符串文本,下面将字符串内容转换为大写字母
        exp = spel.parseExpression("new String('Hello,world').toUpperCase()");
        message = (String) exp.getValue();
        System.out.println(message);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • SpEL 表达式可以与 XML 或基于注解的配置元数据一起使用,SpEL 表达式以#{开头,以}结尾,如#{'Hello'}

    • 可以使用以下表达式来设置属性或构造函数的参数值,如以下代码。

    • 举例理解:基于xml配置

    • 实体类

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private String name;
        private int age;
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 编写配置文件applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean class="com.zk.entity.User" id="user">
            <constructor-arg name="name" value="#{'tony'}"/>
            <constructor-arg name="age" value="12"/>
            <constructor-arg name="password" value="#{T(java.lang.Math).random() * 100.0}"/>
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 测试
    @Test
    public void testDemo01(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口
        User user = context.getBean("user", User.class);
        System.out.println(user.toString());
    
    }
    //User(name=tony, age=12, password=95.40243135135951)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 基于注解配置类,@Value 注解可以放在字段、方法、以及构造函数参数上,以指定默认值。

    • 实体类

    @Component
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        @Value("#{'tony'}")
        private String name;
        @Value("12")
        private int age;
        @Value("#{T(java.lang.Math).random() * 100.0}")
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 配置类
    @Configuration
    @ComponentScan("com.zk")
    @EnableAspectJAutoProxy
    public class MyConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 测试及结果
    @Test
    public void Test01(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User user = (User)context.getBean("user");
        System.out.println(user.getName()+"=="+user.getPassword());
    }
    //tony==95.12492710363902
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 通过名称引用其它 Bean 属性,如以下代码
    @Component
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Web {
        @Value("#{user.name}")
        private String account;
        @Value("#{user.password}")
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 测试及结果
    @Test
    public void testDemo02(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        //动态代理的是接口
        Web web = context.getBean("web", Web.class);
        System.out.println(web.toString());
    }
    //Web(account=tony, password=4.006748130526272)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 使用 SpEL进行运算
    //使用 SpEL进行运算
    SpelExpressionParser spel = new SpelExpressionParser();
    String message = (String)spel.parseExpression("'Hello world'+', zk coming'").getValue();
    System.out.println(message);
    
    Integer num = (Integer)spel.parseExpression("50-28%5").getValue();
    System.out.println(num);
    
    boolean flag = (boolean)spel.parseExpression("true and false").getValue();
    System.out.println(flag);
    
    boolean flag1 = (boolean)spel.parseExpression("'Hello world'.length == 6").getValue();
    System.out.println(flag1);
    
    String date = (String)spel.parseExpression("new java.util.Date().toLocaleString()").getValue();
    System.out.println(date);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    Hello world, zk coming
    47
    false
    false
    2022-6-27 18:35:31
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 在 SpEL 中,我们可以定义变量,并在方法中使用。定义变量需要用到 StandardEvaluationContext 类。

    一个强大且高度可配置的EvaluationContext实现。此上下文使用所有适用策略的标准实现,基于反射来解析属性、方法和字段

    @Component
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        @Value("#{'tony'}")
        private String name;
        @Value("12")
        private int age;
        @Value("#{T(java.lang.Math).random() * 100.0}")
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 测试及结果
    @Test
    public void TestVal(){
        User user = new User();
        StandardEvaluationContext context = new StandardEvaluationContext(user);
        SpelExpressionParser spel = new SpelExpressionParser();
        spel.parseExpression("name").setValue(context,"张三");
        System.out.println(user.toString());
    }
    //User(name=张三, age=0, password=null)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    本专栏完:具体请到C语言中文网学习

  • 相关阅读:
    elementui 更换主题色
    ant design vue:自定义锚点样式
    产品经理如何做项目管理?
    冷热电气多能互补的微能源网鲁棒优化调度(Matlab代码实现)
    ENVI_IDL: 批量制作专题地图
    gitlab自定义头像设置
    JDBC教程
    在乡村沃土上,架起一座直播间
    损失函数概论(机器学习)
    RocketMQ的push消费方式实现的太聪明了
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/125530350