你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

SpEL Collection访问

2022/1/1 8:06:36

Collection访问

声明

首先这个系列的本篇博客参照油管Spring Expression Language教学视频Accessing Collection而写。

开始

  • Individual elements within a List are accessed by using []notation.
  • Filter operations on elements in a List are performed using .?[] notation.
  • Projection of elements in a List is performed using .![] notation.
  • Individual elements within a Map are aceessed by referring to the corresponding key using [] notation.

接下来的例子将一一演示

同样是一个检查学生是否及格的例子

public class Student {
    private String studentName;
    private Integer marks;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getMarks() {
        return marks;
    }

    public void setMarks(Integer marks) {
        this.marks = marks;
    }
}

import java.util.List;

public class StudentListAccessor {
    private Student thirdStudentInList;
    private List<Student> failedStudents;
    private List<String> studentNames;

    public Student getThirdStudentInList() {
        return thirdStudentInList;
    }

    public void setThirdStudentInList(Student thirdStudentInList) {
        this.thirdStudentInList = thirdStudentInList;
    }

    public List<Student> getFailedStudents() {
        return failedStudents;
    }

    public void setFailedStudents(List<Student> failedStudents) {
        this.failedStudents = failedStudents;
    }

    public List<String> getStudentNames() {
        return studentNames;
    }

    public void setStudentNames(List<String> studentNames) {
        this.studentNames = studentNames;
    }
}

主要还是看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.xsd">

    <bean id="student1" class="AccessingCollection.Student">
        <property name="studentName" value="Jack"></property>
        <property name="marks" value="70"></property>
    </bean>

    <bean id="student2" class="AccessingCollection.Student">
        <property name="studentName" value="Sean"></property>
        <property name="marks" value="30"></property>
    </bean>

    <bean id="student3" class="AccessingCollection.Student">
        <property name="studentName" value="Jimmy"></property>
        <property name="marks" value="20"></property>
    </bean>

    <bean id="studentList" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
                <ref bean="student3"></ref>
            </list>
        </constructor-arg>
    </bean>

    <bean id="studentListAccessor" class="AccessingCollection.StudentListAccessor">
        <property name="thirdStudentInList" value="#{studentList[2]}"></property>
        <property name="failedStudents" value="#{studentList.?[marks lt 40]}"></property>
        <property name="studentNames" value="#{studentList.![studentName]}"></property>
    </bean>
</beans>

这里重点说一说几个地方、

<property name="failedStudents" value="#{studentList.?[marks lt 40]}"></property>

这里为什么不写[marks < 40]呢?你第一反应是不是想到shell?😋

因为是XMl的规范,所以有些字符需要使用XML Escape characters替换,不过奇怪的是这里的报错是不给用<,我试了>是可以的。

然后就是说一说这个.![...]

<property name="studentNames" value="#{studentList.![studentName]}"></property>

这个在Spring的文档里也有一个例子,就是说我们想要得到一个collection里的某个property时可以用.![projectionExpression],并且返回的结果同样也是一个List,就像上面代码里的我们想要得到一个collection(studentList)的某个property(studentName)

那么最后跑一个Demo

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

import java.util.List;

public class AccessingCollectionDemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("accessingCollection.xml");
        StudentListAccessor studentListAccessor = applicationContext.getBean("studentListAccessor", StudentListAccessor.class);
        Student student = studentListAccessor.getThirdStudentInList();
        System.out.println(student.getStudentName() + "\t" + student.getMarks());
        System.out.println("--------------------");

        List<String> studentNames = studentListAccessor.getStudentNames();
        for(String name : studentNames){
            System.out.println(name);
        }

        System.out.println("List of failed Students");
        List<Student> failedStudents = studentListAccessor.getFailedStudents();
        for(Student s : failedStudents){
            System.out.println(s.getStudentName() + "\t" + s.getMarks());
        }
    }
}

Map

map的话其实也非常容易理解,通过key来取得value

一个电话📞簿的例子

public class TelephoneDirectoryAccessor {
    private Integer telephoneNumber;

    public Integer getTelephoneNumber() {
        return telephoneNumber;
    }

    public void setTelephoneNumber(Integer telephoneNumber) {
        this.telephoneNumber = telephoneNumber;
    }
}

接下来的都在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="telephoneDirectory" class="java.util.HashMap">
        <constructor-arg>
            <map>
                <entry key="Jack" value="112"></entry>
                <entry key="Jimmy" value="221"></entry>
            </map>
        </constructor-arg>
    </bean>

    <bean id="telephoneDirectoryAccessor" class="AccessingCollection.Map.TelephoneDirectoryAccessor">
        <property name="telephoneNumber" value="#{telephoneDirectory['Jack']}"></property>
    </bean>
</beans>

可以看一下怎么取值就行了

<bean id="telephoneDirectoryAccessor" class="AccessingCollection.Map.TelephoneDirectoryAccessor">
        <property name="telephoneNumber" value="#{telephoneDirectory['Jack']}"></property>
    </bean>

跑一下Demo

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

public class TelephoneDirectoryAccessorDemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("accessingMap.xml");
        TelephoneDirectoryAccessor telephoneDirectoryAccessor = applicationContext.getBean("telephoneDirectoryAccessor", TelephoneDirectoryAccessor.class);
        System.out.println(telephoneDirectoryAccessor.getTelephoneNumber());
    }
}

那么SpEL系列就到此结束了,想要知道更多的呢,就去看Spring的SpEL文档吧