• 6.SpringEL与List,Map


    SpringEL与List,Map

    介绍

    使用SpEL与 Map 和 List 的工作方式与Java是完全一样的

    //get map whete key = 'MapA'
    @Value("#{testBean.map['MapA']}")
    private String mapA;
    
    //get first value from list, list is 0-based.
    @Value("#{testBean.list[0]}")
    private String list;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Spring EL以注解的形式

    在这里,创建了一个HashMap和ArrayList,并添加了一些初始测试数据

    package com.yiibai.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("customerBean")
    public class Customer {
    
    	@Value("#{testBean.map['MapA']}")
    	private String mapA;
    
    	@Value("#{testBean.list[0]}")
    	private String list;
    
    	public String getMapA() {
    		return mapA;
    	}
    
    	public void setMapA(String mapA) {
    		this.mapA = mapA;
    	}
    
    	public String getList() {
    		return list;
    	}
    
    	public void setList(String list) {
    		this.list = list;
    	}
    
    	@Override
    	public String toString() {
    		return "Customer [mapA=" + mapA + ", list=" + list + "]";
    	}
    
    }
    
    • 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 java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.springframework.stereotype.Component;
    
    @Component("testBean")
    public class Test {
    
    	private Map<String, String> map;
    	private List<String> list;
    
    	public Test() {
    		map = new HashMap<String, String>();
    		map.put("MapA", "This is MapA");
    		map.put("MapB", "This is MapB");
    		map.put("MapC", "This is MapC");
    
    		list = new ArrayList<String>();
    		list.add("List0");
    		list.add("List1");
    		list.add("List2");
    
    	}
    
    	public Map<String, String> getMap() {
    		return map;
    	}
    
    	public void setMap(Map<String, String> map) {
    		this.map = map;
    	}
    
    	public List<String> getList() {
    		return list;
    	}
    
    	public void setList(List<String> list) {
    		this.list = list;
    	}
    
    }
    
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    执行程序

    Customer obj = (Customer) context.getBean("customerBean");
    System.out.println(obj);
    
    • 1
    • 2

    输出结果:

    Customer [mapA=This is MapA, list=List0]
    
    • 1

    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="mapA" value="#{testBean.map['MapA']}" />
    		<property name="list" value="#{testBean.list[0]}" />
    	bean>
    
    	<bean id="testBean" class="com.yiibai.core.Test" />
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    log日志异常堆栈打印的正确姿势
    Invoking “make -pkg test -j4 -l4“failed catkin_make时错误
    晶振与定时时间的简单理解
    【专栏】RPC系列(番外)-“土气”的IO实现
    LintCode 511: Swap Two Nodes in Linked List (链表好题)
    【Golang】Gin处理GET、POST请求
    Qt优秀开源项目之九:qTox
    【机考】华为OD2022.11.01机考题目思路与代码
    华为OD:跳房子I
    MacBook Pro M1 安装Burp Suite中文版
  • 原文地址:https://blog.csdn.net/LuckFairyLuckBaby/article/details/132910089