Spring frame
클래스가 많아지면 복잡해진다. 객체지향 프로그래밍의 근본적인 한계. 극복하기 위해 나온 것이 Spring
POJO
---
Spring Container
---
JVM
IoC
IoC를 이용해서 Tight coupling이 아닌 Loose coupling으로 연결.
A 또는 B를 생성하는 것이 아니라 만든 것을 주입한다.
DI 방법
- 컨스트럭터
- p방법
- Setter 방법
리뷰
- 어노테이션 방법
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.component; import java.util.ArrayList; import javax.annotation.Resource; import com.frame.Dao; import com.frame.Search; import com.frame.Service; import com.vo.Item; @org.springframework.stereotype.Service("itemservice") // Component라고 써되 되지만, Service라고 따로 명시 public class ItemService implements Service<String, Item>, Search<Integer, Integer, Item>{ @Resource(name="itemdao") Dao dao; @Resource(name="itemdao") Search search; public void setDao(Dao dao) { this.dao = dao; } public ItemService() { } public ItemService(Dao dao) { this.dao = dao; } @Override public void register(Item v) { dao.update(v); } @Override public void remove(String k) { dao.delete(k); } @Override public void modify(Item v) { dao.update(v); } @Override public Item get(String k) { return (Item) dao.select(k); } @Override public ArrayList<Item> get() { return dao.select(); } @Override public ArrayList<Item> search(Integer k) { return search.search(k); } @Override public ArrayList<Item> search(Integer k1, Integer k2) { return search.search(k1,k2); } } | cs |
프로젝트시
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.frame; import java.util.ArrayList; public interface Dao<K, V> { public abstract void insert(V v); public abstract void update(V v); public abstract void delete(K k); public abstract V select(K k); public abstract ArrayList<V> select(); } | cs |
1 2 3 4 5 | public void register(User v) { System.out.println("Log Start ..." + v.getId()); dao.insert(v); System.out.println("Log End ..." + v.getId()); } | cs |
AOP(Aspect Oriented Programming)
응집도와 관련.핵심 비즈니스 로직은 몇개 안됨. 로깅, 예외처리, 트랜잭션 처리같은 부가적인 코드가 많음 -> method의 복잡도는 커짐
횡단 관심
XML에 어떤 함수가 실행되기 전에 공통기능을 사용하도록 한다.
12345 public void register(User v) {System.out.println("Log Start ..." + v.getId()); // 횡단 관심dao.insert(v); // 핵심 비즈니스 로직System.out.println("Log End ..." + v.getId()); // 횡단 관심}cs
방법
- XML(까다롭고 복잡)
- Annotation (Simple 좋음)
실습
1 2 3 4 5 6 7 8 | package com.log; public class LogAdvice { public void printLog() { System.out.println("[LOG]: Business Logic ..."); } } | cs |
횡단관심 세팅
라이브러리에 필요한 것들 추가되어야함
XML 세팅 부분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?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:aop="http://www.springframework.org/schema/aop" <- 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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <- AOP 추가 <bean id="log" class="com.log.LogAdvice"/> <- 사용할 횡단 관심 <bean id="userdao" class="com.component.UserDao"/> <bean id="itemdao" class="com.component.ItemDao"/> <bean id="userservice" class="com.component.UserService"> <property name="dao" ref="userdao"></property> </bean> <bean id="itemservice" class="com.component.ItemService"> <property name="dao" ref="itemdao"></property> </bean> <aop:config> <aop:pointcut id="mypoint" expression="execution(* com.component.*Service.*(..))" /> <- 들어갈 위치 <aop:aspect ref="log"> <aop:before pointcut-ref="mypoint" method="printLog"/> <- 위치에 친구를 쓴다. </aop:aspect> </aop:config> </beans> | cs |
실습 2
새로운 횡단관심
1 2 3 4 5 6 7 8 | package com.log; public class TxAdvice { public void startTx() { System.out.println("Start Transaction....."); } } | cs |
XML에 위치 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?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: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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="log" class="com.log.LogAdvice"/> <bean id="tx" class="com.log.TxAdvice"/> <bean id="userdao" class="com.component.UserDao"/> <bean id="itemdao" class="com.component.ItemDao"/> <bean id="userservice" class="com.component.UserService"> <property name="dao" ref="userdao"></property> </bean> <bean id="itemservice" class="com.component.ItemService"> <property name="dao" ref="itemdao"></property> </bean> <aop:config> <aop:pointcut id="mypoint2" expression="execution(* com.component.*Dao.*(..))" /> <aop:pointcut id="mypoint" expression="execution(* com.component.*Service.*(..))" /> <aop:aspect ref="log"> <aop:before pointcut-ref="mypoint" method="printLog"/> <aop:after pointcut-ref="mypoint2" method="printLog2"/> </aop:aspect> <aop:aspect ref="tx"> <aop:before pointcut-ref="mypoint" method="startTx"/> </aop:aspect> </aop:config> </beans> | cs |
Expression 부분의 위치는 정확히 표현. p170 확인
<aop:after> 오류 발생과 상관없이 그냥 실행.
Q. Exception이 발생할 떄 실행하도록.
빅데이터 분석
성향분석 -> 로그를 남김 -> 사이트 배치를 바꿈.
after - 아무때나
after-throwing - 오류가 날때만
after-returning 정상일때만
1 2 3 | <aop:after pointcut-ref="mypoint2" method="printLog2"/> // 아무때나 <aop:after-throwing pointcut-ref="mypoint2" method="printLog2"/> //오류가 날때 <aop:after-returning pointcut-ref="mypoint2" method="printLog2"/> //정상일때 | cs |
Around
실행되기 전 후에 무언가 하도록 한다.
실습
XML 설정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?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: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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="log" class="com.log.LogAdvice"/> <bean id="tx" class="com.log.TxAdvice"/> <bean id="userdao" class="com.component.UserDao"/> <bean id="itemdao" class="com.component.ItemDao"/> <bean id="userservice" class="com.component.UserService"> <property name="dao" ref="userdao"></property> </bean> <bean id="itemservice" class="com.component.ItemService"> <property name="dao" ref="itemdao"></property> </bean> <aop:config> <aop:pointcut id="mypoint2" expression="execution(* com.component.*Dao.*(..))" /> <aop:pointcut id="mypoint" expression="execution(* com.component.*Service.*(..))" /> <aop:aspect ref="log"> <aop:before pointcut-ref="mypoint" method="printLog"/> <aop:after-returning pointcut-ref="mypoint2" method="printLog2"/> </aop:aspect> <aop:aspect ref="tx"> <aop:around pointcut-ref="mypoint" method="aroundTx"/> <-수정된 부분 </aop:aspect> </aop:config> </beans> | cs |
around내용 정의
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.log; import org.aspectj.lang.ProceedingJoinPoint; public class TxAdvice { public void startTx() { System.out.println("Start Transaction....."); } public Object aroundTx(ProceedingJoinPoint pjp) throws Throwable { Object result = null; System.out.println("aroundTx start....."); // 전에 할 일 result = pjp.proceed(); // 메인 비즈니스 로직 System.out.println("aroundTx end....."); // 후에 할 일 return result; } } | cs |
Q. 어떤 함숙 ㅏ들어왔는지 확인하기 위해서 -> JoinPoint 사용
1 2 3 4 5 | public void printLog(JoinPoint jp) { String method = jp.getSignature().getName(); Object [] args = jp.getArgs(); System.out.println("[LOG]:"+method+" "+args[0].toString()); } | cs |
Q. 어떤 함수가 들왔는지 + 어떤 Return 하는지
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.log; import org.aspectj.lang.JoinPoint; public class LogAdvice { public void printLog(JoinPoint jp) { String method = jp.getSignature().getName(); Object [] args = jp.getArgs(); System.out.println("[LOG]:"+method+" "+args[0].toString()); } public void printLog2(JoinPoint jp, Object returnObj) { String method = jp.getSignature().getName(); System.out.println("[After-Returning LOG]: "+method+" "+returnObj); } } | cs |
Xml 설정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?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: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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="log" class="com.log.LogAdvice"/> <bean id="tx" class="com.log.TxAdvice"/> <bean id="userdao" class="com.component.UserDao"/> <bean id="itemdao" class="com.component.ItemDao"/> <bean id="userservice" class="com.component.UserService"> <property name="dao" ref="userdao"></property> </bean> <bean id="itemservice" class="com.component.ItemService"> <property name="dao" ref="itemdao"></property> </bean> <aop:config> <aop:pointcut id="mypoint2" expression="execution(* com.component.*Dao.*(..))" /> <aop:pointcut id="mypoint" expression="execution(* com.component.*Service.*(..))" /> <aop:aspect ref="log"> <aop:before pointcut-ref="mypoint" method="printLog"/> <aop:after-returning pointcut-ref="mypoint2" method="printLog2" returning="returnObj"/> </aop:aspect> </aop:config> </beans> | cs |
댓글
댓글 쓰기