20 Spring EL Lists,Maps实例

作者: 笑Skr人啊 | 来源:发表于2017-08-21 17:23 被阅读7次

在这篇文章中,我们将介绍如何使用Spring EL从 Map 和 List 中获得值。事实上,使用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;

Spring EL以注解的形式

package com.gp6.el.listsAndMaps;

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 + "]";
    }

}

package com.gp6.el.listsAndMaps;

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 TestBean {

    private Map<String, String> map;
    private List<String> list;

    public TestBean() {
        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;
    }

}

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.gp6.el.listsAndMaps" />
</beans>
package com.gp6.el.listsAndMaps;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

     public static void main( String[] args ) {
            
         ApplicationContext context = new ClassPathXmlApplicationContext(
                    "com/gp6/el/listsAndMaps/el_lists_maps.xml");

         Customer customer = (Customer) context.getBean("customerBean");
         System.out.println(customer);
    }
    
}

输出:

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

以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.gp6.el.listsAndMaps.Customer">
        <property name="mapA" value="#{testBean.map['MapA']}" />
        <property name="list" value="#{testBean.list[0]}" />
    </bean>

    <bean id="testBean" class="com.gp6.el.listsAndMaps.TestBean" />

</beans> 

相关文章

网友评论

    本文标题:20 Spring EL Lists,Maps实例

    本文链接:https://www.haomeiwen.com/subject/xykhdxtx.html