Spring framework SPEL

이전영·2022년 3월 15일
0

스프링 selection, projection

user.xml 파일코드

<!--  
	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>

MainClass

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);
		}
		
	}
profile
개발자 2년차

0개의 댓글

관련 채용 정보