• 3.SpringEL方法调用实例


    SpringEL方法调用实例

    介绍

    Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用SpEL方法调用”

    Spring EL在注解的形式

    了解如何实现Spring EL方法调用与@Value注释

    package com.yiibai.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("customerBean")
    public class Customer {
    
    	@Value("#{'yiibai'.toUpperCase()}")
    	private String name;
    
    	@Value("#{priceBean.getSpecialPrice()}")
    	private double amount;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public double getAmount() {
    		return amount;
    	}
    
    	public void setAmount(double amount) {
    		this.amount = amount;
    	}
    
    	@Override
    	public String toString() {
    		return "Customer [name=" + name + ", amount=" + amount + "]";
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    package com.yiibai.core;
    
    import org.springframework.stereotype.Component;
    
    @Component("priceBean")
    public class Price {
    
    	public double getSpecialPrice() {
    		return new Double(199.09);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出

    Customer [name=YIIBAI, amount=199.09]
    
    • 1

    在字串文本上调用 toUpperCase()方法

    @Value("#{'yiibai'.toUpperCase()}")
    private String name;
    
    • 1
    • 2

    在 ‘priceBean’,Bean上调用getSpecialPrice() 方法

    @Value("#{priceBean.getSpecialPrice()}")
    private double amount;
    
    • 1
    • 2

    SpringEL调用List,Map中的方法

    从List中过滤元素

    假设你有一个包含各种字符串的List,你想使用SpEL来过滤出包含特定字符的元素。你可以使用contains方法来实现这个功能。

    import org.springframework.expression.Expression;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyComponent {
        private List<String> myList = Arrays.asList("apple", "banana", "cherry", "date");
    
        public void filterList(String searchTerm) {
            ExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("myList[#root.contains(value, 'searchTerm')]");
            List<String> filteredList = (List<String>) exp.getValue(myList, Object[].class);
            System.out.println(filteredList);  // prints: [banana, cherry]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这个例子中,我们使用了SpEL的contains函数来检查列表中的每个元素是否包含搜索词。如果是,那么这个元素就会被包含在结果列表中

    从Map中获取值

    假设你有一个包含键值对的Map,你想使用SpEL来获取特定键的值。你可以直接使用Map的键作为SpEL表达式的一部分来获取值。

    import org.springframework.expression.Expression;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyComponent {
        private Map<String, Integer> myMap = new HashMap<>();
        myMap.put("apple", 1);
        myMap.put("banana", 2);
        myMap.put("cherry", 3);
        myMap.put("date", 4);
    
        public void getValueFromMap(String key) {
            ExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("myMap[key]");
            Integer value = (Integer) exp.getValue(myMap, Object.class);
            System.out.println(value);  // prints: 2
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这个例子中,我们使用了SpEL来获取Map中特定键的值。我们直接在SpEL表达式中使用键名作为索引来访问Map的值。

    注意:这些示例需要在Spring框架的环境中运行,因为SpEL是Spring的一部分。

    Spring EL在XML的形式

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     
    	<bean id="customerBean" class="com.yiibai.core.Customer">
    		<property name="name" value="#{'yiibai'.toUpperCase()}" />
    		<property name="amount" value="#{priceBean.getSpecialPrice()}" />
    	bean>
     
    	<bean id="priceBean" class="com.yiibai.core.Price" />
    	
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    输出

    Customer [name=YIIBAI, amount=199.09]
    
    • 1
  • 相关阅读:
    大语言模型的关键技术
    C++ opencv图像存储和MAT容器
    客户转化率太低?CRM客户管理系统来帮您
    《数据库系统概念》——关系数据库
    快应用接入帐号服务指导
    完美十进制数——去年天梯校赛
    基于快速行进平方法的水面无人船路径规划
    Pytorch源码编译Libtorch
    AtCoder Beginner Contest 311 D(dfs/bfs)
    《Effective Java》第9条:try-with-resources优先于try-finally
  • 原文地址:https://blog.csdn.net/LuckFairyLuckBaby/article/details/132910020