Server Tomcat v8.5 Server at localhost failed to start.

 

 

 해결방법

 

* Tomcat v8.5 Server at localhost → Server Options → Publish module contexts to separate XML files 체크 후 Ctrl + S (저장)

 

 

 

 출처

 

https://blog.naver.com/PostView.naver?blogId=starry_night1003&logNo=222134335543&parentCategoryNo=&categoryNo=7&viewDate=&isShowPopularPosts=true&from=search 

https://beagle-dev.tistory.com/76

+ 강의 교재

Port 8005 required by Tomcat v8.5 Server at localhost is already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).

 

 

간단한 코드 실행해보려 했는데, 바로 에러 발생

 

 해결 방법

 

1. CMD(명령 프롬프트)창 관리자 권한으로 실행 

Windows Key → CMD 검색 → 명령 프롬프트 앱 마우스로 우클릭 → 자세히 → 관리자 권한으로 실행

2. 

netstat -ano | findstr 8005

taskkill /F /pid ?????

netstat -ano | findstr 8005

 

 

 출처

 

https://devlimk1.tistory.com/151

+ 강의 교재

Spring Apache Tomcat Server 설정

 

Eclipse와 설정 방법이 비슷하다.

까먹어서 다시 정리해보자.

 

 Spring Apache Tomcat Server 설정

 

1.  STS3 실행 → Server 탭 → No servers are availavle. Click this link to create a new server... 마우스로 클릭

 

2. [New Server] → [Apache] → [Tomcat v8.5 Server] → Next 버튼 마우스로 클릭

3. [Tomcat installation directory] → Browse... 버튼 마우스로 클릭하여 apache-tomcat 8.5 저장된 경로 선택 

→ JRE: → Installed JREs... 버튼 클릭하여 설치한 jdk 버전 선택 → Finish 버튼 마우스로 클릭

4. Server 탭 → Tomcat v8.5 Server at localhost [Stopped, Republish] 마우스로 더블 클릭하여 실행

 

5. [Tomcat v8.5 Server at localhost] → [Server Locations] → Use Tomcat installation (takes control of Tomcat installation) 체크 → Deploy path:  → Browse... 버튼 클릭하여 apache-tomcat-8.5.xx\webapps 폴더 선택 후 Ctrl + s로 설정 저장

 

 출처

 

 

+ 강의 교재

 

해당 프로젝트 F5로 새로고침 진행해봐도 안되서 서버 삭제 후 새로 만듬

실습 5일차_AOP

 

 

 Spring AOP

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service = ctx.getBean("userService", UserService.class);
		System.out.println("반환값:"+ service.insert());

	}
}

MainClass2.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.DeptService;

public class MainClass2 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		DeptService service = ctx.getBean("deptService", DeptService.class);
		System.out.println("반환값:"+ service.selectList());

	}
}

MainClass3.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.DeptService;
import com.service.EmpService;

public class MainClass3 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		EmpService service = ctx.getBean("empService", EmpService.class);
		try {
		  service.update();
		}catch(Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
	}
}

MainClass4.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.EmpService;
import com.service.PersonService;

public class MainClass4 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		PersonService service = ctx.getBean("personService", PersonService.class);
		try {
		  String str = service.delete();
		  System.out.println("반환값:"+str);
		}catch(Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
	}
}

 

DeptServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class DeptServiceAspect {

	@AfterReturning(pointcut = "execution(* selectList(..))", returning = "retVal")
	public void doAccessCheck(Object retVal) {
		System.out.println("DeptServiceAspect.AfterReturning.selectList");
		System.out.println("리턴값:" + retVal);
	}
}

EmpServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

//부가기능 처리: aspect
@Aspect
public class EmpServiceAspect {

	@AfterThrowing(pointcut = "execution(* update(..))", throwing = "ex")
	public void doRecoveryActions(Exception ex) {
		System.out.println("EmpServiceAspect.AfterThrowing" 
	     + ex.getMessage());
	}
}

PersonServiceAspect.java

package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

//부가기능 처리: aspect
@Aspect
public class PersonServiceAspect {
 
		@Around("execution(* delete(..))")
	    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
	        System.out.println("Before Advice");
	        Object retVal = pjp.proceed();
	        System.out.println("After Advice");
	        System.out.println("retVal:" + retVal);
	        return retVal;
	    }
}

UserServiceAspect.java

package com.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

//부가기능 처리: aspect
@Aspect
public class UserServiceAspect {
  //1. pointcut와 advice 분리
	@Pointcut("execution(* list(..))")// the pointcut expression
	private void anyOldTransfer() {}// the pointcut signature
	
	@Before("anyOldTransfer()")
    public void doAccessCheck(JoinPoint point) {
		System.out.println("point:" + point);
		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.list");
    }
	
	//2. pointcut와 advice 합치기
	@Before("execution(* insert*(..))")
    public void doAccessCheck2(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.insert");
    }
	
	@After("execution(* insert*(..))")
    public void doAccessCheck3(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.AfterAdvice.insert");
    }
}

 

DeptService.java

package com.service;
import java.util.Arrays;
import java.util.List;

//타겟 (target object) ==> 핵심기능
public class DeptService {

	public List<String> selectList(){
		System.out.println("핵심기능:DeptService.selectList");
		return Arrays.asList("AAA","BBB");
	}
}

EmpService.java

package com.service;

//타겟 (target object) ==> 핵심기능
public class EmpService {

	public void update() {
		int num = 10/2;
		System.out.println("핵심기능:EmpService.update");
	}
}

PersonService.java

package com.service;

//타겟 (target object) ==> 핵심기능
public class PersonService {

	public String delete() {
		int num = 10/0;
		System.out.println("핵심기능:PersonService.insert");
		return "Hello";
	}
}

UserService.java

package com.service;

//타겟 (target object) ==> 핵심기능
public class UserService {

	//pointcut
	public String list() {
		return "UserService.list";
	}

	public String insert() {
		System.out.println("핵심기능:UserService.insert");
		return "UserService.insert";
	}
	public String insert2() {
		return "UserService.insert2";
	}	
}

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

	<aop:aspectj-autoproxy />
	<!-- 1. BeforeAdvice와 AfterAdvice 실습 -->
	<bean id="userService" class="com.service.UserService" />
	<bean id="userAspect" class="com.aspect.UserServiceAspect" />
	
	<!-- 2. AfterReturningAdvice 실습 -->
	<bean id="deptService" class="com.service.DeptService" />
	<bean id="deptAspect" class="com.aspect.DeptServiceAspect" />
	
	<!-- 3 AfterThrowingAdvice 실습 -->
	<bean id="empService" class="com.service.EmpService" />
	<bean id="empAspect" class="com.aspect.EmpServiceAspect" />
	
	<!-- 4 AroundAdvice 실습 -->
	<bean id="personService" class="com.service.PersonService" />
	<bean id="personAspect" class="com.aspect.PersonServiceAspect" />
</beans>

 

* aop 추가 방법

[Namespaces] → [Configure Namespaces] 팝업창 OK 버튼 마우스로 클릭

 

* 출력 화면 

MainClass 출력화면

 

MainClass2 출력화면

 

MainClass3 출력화면

 

MainClass4 출력화면

 

 Spring AOP Component_Scan

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
			
		UserService service = ctx.getBean("userService", UserService.class);
		
		System.out.println(service.insert());
		//System.out.println(service.list());

	}
}

MainClass2.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.DeptService;
import com.service.UserService;

public class MainClass2 {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
				
		DeptService service2 = ctx.getBean("deptService", DeptService.class);
		System.out.println("반환값:" + service2.selectList());
		//System.out.println(service.list());
		
	}
}

MainClass3.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.EmpService;

public class MainClass3 {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
			
		EmpService service = ctx.getBean("empService", EmpService.class);
			
		try {
			service.update();
		} catch (Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
		
	}
}

MainClass4.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.EmpService;
import com.service.PersonService;

public class MainClass4 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		PersonService service = ctx.getBean("personService", PersonService.class);
		try {
		  String str = service.delete();
		  System.out.println("반환값:"+str);
		}catch(Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
	}
}

 

DeptServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class DeptServiceAspect {

	@AfterReturning(pointcut = "execution(* selectList(..))", returning = "retVal")
	public void doAccessCheck(Object retVal) {
		System.out.println("DeptServiceAspect.AfterReturning.selectList");
		System.out.println("리턴값:" + retVal);
	}
}

EmpServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

//부가기능 처리: aspect
@Component
@Aspect
public class EmpServiceAspect {

	@AfterThrowing(pointcut = "execution(* update(..))", throwing = "ex")
	public void doRecoveryActions(Exception ex) {
		System.out.println("EmpServiceAspect.AfterThrowing" 
	     + ex.getMessage());
	}
}

PersonServiceAspect.java

package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

//부가기능 처리: aspect
@Component
@Aspect
public class PersonServiceAspect {
 
	 @Around("execution(* delete(..))")
	    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
	        System.out.println("Before Advice");
	        Object retVal = pjp.proceed();
	        System.out.println("After Advice");
	        System.out.println("retVal:" + retVal);
	        return retVal;
	    }
}

UserServiceAspect.java

package com.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//부가기능 처리: aspect
@Component
@Aspect
public class UserServiceAspect {
  //1. pointcut와 advice 분리
	@Pointcut("execution(* list(..))")// the pointcut expression
	private void anyOldTransfer() {}// the pointcut signature
	
	@Before("anyOldTransfer()")
    public void doAccessCheck(JoinPoint point) {
		System.out.println("point:" + point);
		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.list");
    }
	
	//2. pointcut와 advice 합치기
	@Before("execution(* insert*(..))")
    public void doAccessCheck2(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.insert");
    }
	
	@After("execution(* insert*(..))")
    public void doAccessCheck3(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.AfterAdvice.insert");
    }
}

 

DeptService.java

package com.service;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;

//타켓(target object) ==> 핵심기능
@Service
public class DeptService {
	public List<String> selectList(){
		System.out.println("핵심기능:DeptService.selectList");
		return Arrays.asList("AAA", "BBB");
	
	}
}

EmpService.java

package com.service;
import org.springframework.stereotype.Service;

//타켓(target object) ==> 핵심기능
@Service
public class EmpService {
	public void update() {
		int num = 10/2;
		System.out.println("핵심기능:EmpService.update");
	}
}

PersonService.java

package com.service;
import org.springframework.stereotype.Service;

// 타겟 (target object) ==> 핵심기능
@Service
public class PersonService {
	public String delete() {
		int num = 10/2;
		System.out.println("핵심기능:PersonSerivce.insert");
		return "Hello";
	}
}

UserService.java

package com.service;
import org.springframework.stereotype.Service;

// 타켓(target object) ==> 핵심기능
@Service
public class UserService {

	// pointcut
	public String list() {
		return "UserService.list";
	}
	
	public String insert() {
		return "UserService.insert";
	}
	
	public String insert2() {
		return "UserService.insert2";
	}
}

 

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

	<aop:aspectj-autoproxy />
	<context:component-scan base-package="com.*"></context:component-scan>
</beans>

 

* 출력 화면 

MainClass 출력화면
MainClass2 출력화면
MainClass3 출력화면
MainClass4 출력화면

 

 출처

 

AOP : 

+ 강의 교재

 

실습 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>

 

* 출력 화면 

 

 출처

 

 

+ 강의 교재

 

+ Recent posts