실습 5일차_SpEL
https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/expressions.html
Spring SpEL_Collection_Selection
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.ListPerson;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
ListPerson listPerson = ctx.getBean("list1", ListPerson.class); //System.out.println(list1);
List<Person> list1 = listPerson.getList();
System.out.println("1. 전체 데이터 출력");
for (Person person : list1) {
System.out.println(person);
}
ListPerson listPerson2 = ctx.getBean("list2", ListPerson.class); //System.out.println(list1);
List<Person> list2 = listPerson2.getList();
System.out.println("2. SpEL 전체 데이터 출력");
for (Person person : list2) {
System.out.println(person);
}
ListPerson listPerson3 = ctx.getBean("list3", ListPerson.class); //System.out.println(list1);
List<Person> list3 = listPerson3.getList();
System.out.println("3. SpEL 특정 인덱스 데이터 출력");
for (Person person : list3) {
System.out.println(person);
}
ListPerson listPerson4 = ctx.getBean("list4", ListPerson.class); //System.out.println(list1);
List<Person> list4 = listPerson4.getList();
System.out.println("4. SpEL 조건 데이터 출력");
for (Person person : list4) {
System.out.println(person);
}
ListPerson listPerson5 = ctx.getBean("list5", ListPerson.class); //System.out.println(list1);
List<Person> list5 = listPerson5.getList();
System.out.println("5. SpEL 멀티 조건 데이터 출력");
for (Person person : list5) {
System.out.println(person);
}
}
}
ListPerson.java
package com.dto;
import java.util.List;
public class ListPerson {
List<Person> list;
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
@Override
public String toString() {
return "ListPerson [list=" + list + "]";
}
}
Person.java
package com.dto;
public class Person {
String username;
int age;
boolean married;
double weight;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", married=" + married + ", weight=" + weight + "]";
}
}
user.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<util:list id="personList">
<bean id="p1" class="com.dto.Person" p:username="고길동" p:age="30" p:married="true" p:weight="50" />
<bean id="p2" class="com.dto.Person" p:username="신길동" p:age="40" p:married="false" p:weight="60" />
<bean id="p3" class="com.dto.Person" p:username="홍길동" p:age="10" p:married="false" p:weight="70" />
<bean id="p4" class="com.dto.Person" p:username="이길동" p:age="25" p:married="true" p:weight="60" />
</util:list>
<bean id="list1" class="com.dto.ListPerson">
<property name="list" ref="personList" />
</bean>
<!-- SpEL -->
<bean id="list2" class="com.dto.ListPerson">
<property name="list" value="#{personList}"/>
</bean>
<!-- SpEL -->
<bean id="list3" class="com.dto.ListPerson">
<property name="list" value="#{personList[1]}"/>
</bean>
<!-- SpEL -->
<!-- 행선택(selection) 문법 ===> 참조값.?[조건식] -->
<bean id="list4" class="com.dto.ListPerson">
<property name="list" value="#{personList.?[age > 25]}"/>
</bean>
<!-- SpEL -->
<!-- 행선택(selection) 문법 ===> 참조값.?[조건식 and 조건식 or 조건식] -->
<bean id="list5" class="com.dto.ListPerson">
<property name="list" value="#{personList.?[age > 25 and married == true]}"/>
</bean>
</beans>
* 출력 화면
Spring SpEL_Collection_Projection
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.ListPerson;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
ListPerson listPerson1 = ctx.getBean("list1", ListPerson.class);
List<String> usernameList = listPerson1.getUsernameList();
System.out.println("1. username 프라퍼티 전체 출력");
for (String name : usernameList) {
System.out.println(name);
}
ListPerson listPerson2 = ctx.getBean("list2", ListPerson.class);
List<Integer> ageList = listPerson2.getAgeList();
System.out.println("2. age 프라퍼티 전체 출력");
for (int age : ageList) {
System.out.println(age);
}
}
}
ListPerson.java
package com.dto;
import java.util.List;
public class ListPerson {
// selection
List<Person> list;
// projection
List<String> usernameList;
List<Integer> ageList;
public List<String> getUsernameList() {
return usernameList;
}
public void setUsernameList(List<String> usernameList) {
this.usernameList = usernameList;
}
public List<Integer> getAgeList() {
return ageList;
}
public void setAgeList(List<Integer> ageList) {
this.ageList = ageList;
}
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
@Override
public String toString() {
return "ListPerson [list=" + list + "]";
}
}
Person.java
package com.dto;
public class Person {
String username;
int age;
boolean married;
double weight;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", married=" + married + ", weight=" + weight + "]";
}
}
user.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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<util:list id="personList">
<bean id="p1" class="com.dto.Person" p:username="홍길동" p:age="30" p:married="true" p:weight="78.6" />
<bean id="p2" class="com.dto.Person" p:username="고길동" p:age="40" p:married="false" p:weight="100.2" />
<bean id="p3" class="com.dto.Person" p:username="신길동" p:age="10" p:married="false" p:weight="56.2" />
<bean id="p4" class="com.dto.Person" p:username="이길동" p:age="23" p:married="true" p:weight="75.6" />
</util:list>
<!--
selection 문법: 참조변수.?[조건식] , 참조변수.?[조건식 and 조건식 or 조건식]
-->
<!--
projection 문법: 참조변수.![property]
-->
<bean id="list1" class="com.dto.ListPerson">
<property name="usernameList" value="#{personList.![username]}" />
</bean>
<bean id="list2" class="com.dto.ListPerson">
<property name="ageList" value="#{personList.![age]}" />
</bean>
</beans>
* 출력 화면
Spring SpEL_Collection_Selection & Projection
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.ListPerson;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
ListPerson listPerson1 = ctx.getBean("list1", ListPerson.class);
List<String> usernameList = listPerson1.getUsernameList();
System.out.println("1. username 프라퍼티 조건 출력");
for (String name : usernameList) {
System.out.println(name);
}
ListPerson listPerson2 = ctx.getBean("list2", ListPerson.class);
List<Integer> ageList = listPerson2.getAgeList();
System.out.println("2. age 프라퍼티 조건 출력");
for (int age : ageList) {
System.out.println(age);
}
}
}
ListPerson.java
package com.dto;
import java.util.List;
public class ListPerson {
// selection
List<Person> list;
// projection
List<String> usernameList;
List<Integer> ageList;
public List<String> getUsernameList() {
return usernameList;
}
public void setUsernameList(List<String> usernameList) {
this.usernameList = usernameList;
}
public List<Integer> getAgeList() {
return ageList;
}
public void setAgeList(List<Integer> ageList) {
this.ageList = ageList;
}
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
@Override
public String toString() {
return "ListPerson [list=" + list + ", usernameList=" + usernameList + ", ageList=" + ageList + "]";
}
}
Person.java
package com.dto;
public class Person {
String username;
int age;
boolean married;
double weight;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", married=" + married + ", weight=" + weight + "]";
}
}
user.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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<util:list id="personList">
<bean id="p1" class="com.dto.Person" p:username="고길동" p:age="30" p:married="true" p:weight="78.6" />
<bean id="p2" class="com.dto.Person" p:username="신길동" p:age="40" p:married="false" p:weight="100.2" />
<bean id="p3" class="com.dto.Person" p:username="홍길동" p:age="10" p:married="false" p:weight="56.2" />
<bean id="p4" class="com.dto.Person" p:username="이길동" p:age="23" p:married="true" p:weight="75.6" />
</util:list>
<!--
selection 문법: 참조변수.?[조건식] , 참조변수.?[조건식 and 조건식 or 조건식]
-->
<!--
projection 문법: 참조변수.![property]
-->
<!--
selection 및 projection 조합
==> 참조변수.?[조건식].![property]
-->
<bean id="list1" class="com.dto.ListPerson">
<property name="usernameList" value="#{personList.?[age > 25].![username]}" />
</bean>
<bean id="list2" class="com.dto.ListPerson">
<property name="ageList" value="#{personList.?[married==true].![age]}" />
</bean>
</beans>
* 출력 화면
출처
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] Could not publish server configuration for Tomcat v8.5 Server at localhost.Multiple Contexts have a path of "/app". (0) | 2022.03.20 |
---|---|
[Spring] 실습 5일차_AOP (0) | 2022.03.20 |
[Spring] 실습 5일차_SpEL (0) | 2022.03.19 |
[Spring] 실습 4일차_MyBatis_Transaction★ (0) | 2022.03.18 |
[Spring] 실습 4일차_MyBatis ★ (0) | 2022.03.18 |