실습 5일차_SpEL
SpEL - Spring Expression Language
Spring Expression Language는 보통 SpEL로 표기하며 구두로는 Spring EL이라고 지칭한다.
SpEL은 보통 객체를 조회하고 조작하는 기능을 제공하여 Unified EL과 유사하지만
Method 호출, 문자열 템플릿 기능 등의 여러가지 추가 기능을 제공하는 표현식 언어이다.
Spring 3.0부터 지원된다.
Spring SpEL
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
Person p2 = ctx.getBean("p2", Person.class);
System.out.println(p2);
}
}
Person.java
package com.dto;
public class Person {
String username;
int age;
boolean isMarried;
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 isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", isMarried=" + isMarried + ", 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="p1" class="com.dto.Person">
<property name="username" value="고길동" />
<property name="age" value="20" />
<property name="married" value="true" />
<property name="weight" value="45.45" />
</bean>
<!-- SpEL -->
<bean id="p2" class="com.dto.Person">
<property name="username" value="#{'신길동'}" />
<property name="age" value="#{20+10}" />
<property name="married" value="#{10>4}" />
<property name="weight" value="#{'75.75'}" />
</bean>
</beans>
* 출력 화면
Spring SpEL_ref
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
Person p2 = ctx.getBean("p2", Person.class);
System.out.println(p2);
}
}
Cat.java
package com.dto;
public class Cat {
String name;
int age;
String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
Person.java
package com.dto;
public class Person {
String username;
int age;
boolean isMarried;
double weight;
Cat cat;
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 isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", isMarried=" + isMarried + ", weight=" + weight
+ ", cat=" + cat + "]";
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="c1" class="com.dto.Cat">
<property name="name" value="나비" />
<property name="age" value="2" />
<property name="sex" value="암컷" />
</bean>
<bean id="p1" class="com.dto.Person">
<property name="username" value="고길동" />
<property name="age" value="20" />
<property name="married" value="true" />
<property name="weight" value="57.25" />
<property name="cat" ref="c1" /> <!-- ref 이전 -->
</bean>
<bean id="p2" class="com.dto.Person">
<property name="username" value="홍길동" />
<property name="age" value="30" />
<property name="married" value="true" />
<property name="weight" value="77.77" />
<property name="cat" value="#{c1}"/> <!-- SpEL -->
</bean>
</beans>
* 출력 화면
Spring SpEL_property
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
Person p2 = ctx.getBean("p2", Person.class);
System.out.println(p2);
}
}
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 getMarried() {
return married;
}
public void setMarried(boolean married) {
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="p1" class="com.dto.Person">
<property name="username" value="고길동" />
<property name="age" value="20" />
<property name="married" value="true" />
<property name="weight" value="45.45" />
</bean>
<!-- SpEL --> <!-- 다른 bean에 있는 property 값 접근할 수 있음 -->
<bean id="p2" class="com.dto.Person">
<property name="username" value="#{p1.username}" />
<property name="age" value="#{p1.age+10}" />
<property name="married" value="#{p1.married}" />
<property name="weight" value="#{p1.weight}" />
</bean>
</beans>
* 출력 화면
Spring SpEL_method
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
Person p1 = ctx.getBean("p1", Person.class);
System.out.println(p1);
Person p2 = ctx.getBean("p2", Person.class);
System.out.println(p2);
Person p4 = ctx.getBean("p4", Person.class);
System.out.println(p4);
}
}
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 getMarried() {
return married;
}
public void setMarried(boolean married) {
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 + "]";
}
// 필요에 의해서 추가된 메서드
public String getName() {
return "GOKilDong";
}
public String getName2() {
//return "GOKilDong";
return null;
}
// static 메서드
public static String getName3() {
return "Park";
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 일반 메서드 호출 방법 -->
<bean id="p1" class="com.dto.Person"> <!-- 필요에 의해서 추가된 메서드에도 접근할 수 있다. SpEL로 메서드 호출 가능 -->
<property name="username" value="#{p1.getName()}" />
<property name="age" value="20" />
<property name="married" value="true" />
<property name="weight" value="45.45" />
</bean>
<!-- SpEL --> <!-- 다른 bean에 있는 property 값 접근할 수 있음 -->
<bean id="p2" class="com.dto.Person">
<property name="username" value="#{p1.getName().toUpperCase()}" />
<property name="age" value="#{p1.age}" />
<property name="married" value="#{p1.married}" />
<property name="weight" value="#{p1.weight}" />
</bean>
<bean id="p3" class="com.dto.Person">
<property name="username" value="#{p1.getName2()?.toUpperCase()}" /><!-- 에러 방지? -->
<property name="age" value="20" />
<property name="married" value="true" />
<property name="weight" value="45.45" />
</bean>
<bean id="p4" class="com.dto.Person"> <!-- static 메서드 호출 방법 -->
<property name="username" value="#{T(com.dto.Person).getName3()}" /><!-- .java가 아니기에 pakage명까지 넣어줘야 함 -->
<property name="age" value="#{T(java.lang.Math).random()+1 * 10.0}" />
<property name="married" value="true" />
<property name="weight" value="45.45" />
</bean>
</beans>
* 출력 화면
출처
SpEL : https://atoz-develop.tistory.com/entry/Spring-SpEL-Spring-Expression-Language
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 5일차_AOP (0) | 2022.03.20 |
---|---|
[Spring] 실습 5일차_SpEL_Collection (0) | 2022.03.19 |
[Spring] 실습 4일차_MyBatis_Transaction★ (0) | 2022.03.18 |
[Spring] 실습 4일차_MyBatis ★ (0) | 2022.03.18 |
[Spring] 실습 4일차_Component_Scan (0) | 2022.03.18 |