* MVC 패턴이란?
UI기반의 application 전체프로그램을 디자인 하기 위한 패턴

* 싱글턴 패턴?
전체 어플리케이션 중 한 클래스를 정의 하기 위한 패턴

DTO(VO) - 데이터를 묶어 주는 역할을 함. 값을 이동시키기 위한 객체

M - model (비지니스 로직 처리) (모델은 2가지로 나뉘어짐. 1. 비지니스로직 - 비지니스Service, 2. DB관련 - DAO )
V - view (응답처리)
C - controller (프로그램의 전체 흐름 처리 work flow)

* 만약 클라이언트가 요청을 하면

1. 요청받기
 -> 요청시 보낸 값을 읽는다.
 -> 검증
2. 비지니스 로직 처리
3. 응답

을 순서대로 처리한다.

비지니스 로직을 밖으로 빼서 처리하고 응답하는 코드도 따로 빼서 만든다.

 

 

 

* Model1 : 컨트롤러 개념만 빠진다.
* Model2 : 컨트롤러 개념 추가.
                Model : Java Beans이 처리
                View : jsp(JAVA코트와 HTML코드 처리)
                controller : Servlet이 처리

* model2는 자바에서만 취급하는 프레임 워크 지만 MVC는 다른 프로그램에서도 사용가능하다.

 

 

 

* 예전에 했던 회원등록 프로그램 적용 예제 

 

 

Posted by 조은성
,

@Aspect 어노테이션을 이용한 AOP

1. @Aspect 어노테이션을 이용하여 Aspect 클래스에 직접 Advice 및 Point등을 직접 설정
2. 설정파일에 <aop:aspectj-autoproxy/> 를 추가 해야함
3. Aspect class를 <bean>으로 등록
4. 어노테이션(Annotation)
 - @Aspect : Aspect 클래스 선언
 - @Before("pointcut")
 - @AfterReturning(pointcut="", returning="")
 - @AfterThrowing(pointcut="",throwing="")
 - @After("pointcut")
 - @Around("pointcut")

5. Around를 제외한 나머지 메소드들은 첫 argument로 JoinPoint를 가질 수 있다.
6. Around 메소드는 argument로 ProceedingJoinPoint를 가질 수 있다.  

 

 

 

 

 

* 결과

------------정상처리----------------
AdviceClass.before.beforeLogger()---------------------
AdviceClass.aroundLogger() 실행----------------
Business메소드가 실행되었습니다.
AdviceClass.afterReturnLogger()--------------
null
AdviceClass.afterLogger()----------------------
------------에러내기----------------
AdviceClass.before.beforeLogger()---------------------
AdviceClass.aroundLogger() 실행----------------
AdviceClass.afterThrowLogger()----------------------
i가 0보다 작다
Exception in thread "main" AdviceClass.afterLogger()----------------------
java.lang.RuntimeException: i가 0보다 작다
 at aop.core.CoreClass.businessMethod(CoreClass.java:6)
 at aop.core.CoreClass$$FastClassByCGLIB$$359776d6.invoke(<generated>)
 at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
 at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
 at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
 at aop.common.AdviceClass.aroundLogger(AdviceClass.java:34)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
 at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
 at aop.core.CoreClass$$EnhancerByCGLIB$$cebdb4e6.businessMethod(<generated>)
 at TestMain.main(TestMain.java:14)

 

Posted by 조은성
,

 

 

 

 

 

 

 

 

* 결과

aop.cust.dao.CustomerDAOImpl.selectCustomerById() : 624ms
aop.cust.dao.CustomerDAOImpl.insertCustomer() : 391ms
aop.cust.service.CustomerServiceImpl.registerCustomer() : 1015ms
-------------등록끝----------------
aop.cust.dao.CustomerDAOImpl.selectCustomerById() : 624ms
aop.cust.dao.CustomerDAOImpl.updateCustomer() : 532ms
aop.cust.service.CustomerServiceImpl.modifyCustomer() : 1156ms
-------------수정끝----------------

 

Posted by 조은성
,

 

 

 

 

 

* 결과

---------before
MemberService.a() 실행
---------after-returning : 리턴값 : null
--------after
---------before
MemberService.c() 실행
---------after-returning : 리턴값 : abc
--------after
main : abc

 

Posted by 조은성
,

 

 

 

 

 

 

 

 

 

* 결과

주문처리시 오류발생
꽃 재고 부족 : 80
꽃 재고 부족
책 1권 판매 ok!

Posted by 조은성
,

 

spring_aop_shop_stu.zip

 

 

 

 

Posted by 조은성
,

 

* MemberService interface만들기

 

 

 

 * aop.xml Spring AOP 만들기

 

 

* 실습

 

 

 

 

 

 

* 결과

MemberServiceImpl.selectMemberById() 실행
------------LoggerAdvice.afterReturnLogger 시작--------------
------------LoggerAdvice.afterReturnLogger 종료--------------
------------LoggerAdvice.afterReturnLogger2 시작--------------
ret : MemberDTO [id=abcde, name=홍길동, age=33]
------------LoggerAdvice.afterReturnLogger2 종료--------------
main() : MemberDTO [id=abcde, name=홍길동, age=1000]
MemberServiceImpl.selectAllMember() 실행
------------LoggerAdvice.afterReturnLogger 시작--------------
------------LoggerAdvice.afterReturnLogger 종료--------------
------------LoggerAdvice.afterReturnLogger2 시작--------------
ret : []
------------LoggerAdvice.afterReturnLogger2 종료--------------
main() : []
MemberServiceImpl.registerMember(mto) 실행
가입도중 오류 발생

 

'프로그래밍 > Spring AOP' 카테고리의 다른 글

AOP around실습 - spring_aop_03_around  (0) 2012.06.25
AOP[실습] - spring_aop_shop_stu  (0) 2012.06.25
Spring_aop[실습]  (0) 2012.06.22
POJO 기반 AOP - AspectJ 표현식  (0) 2012.06.22
proxy패턴(대리인 패턴)  (0) 2012.06.22
Posted by 조은성
,

 

 

 

 

 

 

* 결과

-------------------------------
로그 처리
-------------------------------
고객 등록 처리
-------------------------------
로그 처리
-------------------------------
고객 정보 수정 처리
타겟 객체 이름 : aop.core.CustomerService
-----------인수값-------------
111
sig.getName() : selectMemberNameById
sig.toShortString() : Service.selectMemberNameById(..)
sig.toLongString() : public abstract java.lang.String aop.core.Service.selectMemberNameById(java.lang.String)
-------------------------------
로그 처리
-------------------------------
111의 이름을 조회합니다.
홍길동

 

Posted by 조은성
,

POJO 기반 AOP - AspectJ 표현식

1. AspectJ에서 지원하는 패턴 표현식
2. 스프링은 메서드 호출관련 명시자만 지원

명시자(pattern)
-? 는 생략 가능

3. 명시자
 - execution : 메소드 명을 기준으로 지정
 - within : 타입 명을 기준으로 지정(class 기준)
 - bean : 설정파일에 지정된 빈의 이름(name속성)을 이용해 지정. 2.5버전에 추가됨.(bean설정 기준)

4. 표현

명시자(패턴)
- 패턴은 명시자 마다 다름.
예) execution(public * abc.def..*Service.set*(..)

5. 패턴문자.
 - * : 1개의 모든 값을 표현
   - argument에서 쓰는 경우 :  1개의 argument
   - package에 쓰인 경우 : 1개의 하위 package
   - 이름(메소드, 클래스)에 쓰일 경우 : 모든 글자들
 
 - .. : 0개 이상
   - argument에서 쓰인 경우 :  0개 이상의 argument
   - package에 쓰인 경우 : 0개 이상의 하위 package

6. execution
 - execution(수식어패턴? 리턴타입패턴 패키지패턴?.클래스명패턴.메소드패턴(argument패턴))
 - 수식어패턴 : public, protected, 생략
 - argument에 type을 명시할 경우 객체 타입은 fullyName으로 넣어야 한다.
   - java.lang은 생략가능
 - 위 예 설명
  적용 하려는 메소드들의 패턴은 public 제한자를 가지며 리턴 타입에는 모든 타입이 다 올 수 있다. 이름은 abc.def 패키지와 그 하위 패키지에 있는 모든 클래스 중 Service로 끝나는 클래스들 에서 set을 시작하는 메소드이며 argument는 0개 이상 오며 타입은 상관없다.

7. within
 - within(패키지패턴.클래스명패턴)

8. bean
 - bean(bean이름 패턴)

Posted by 조은성
,

 

 

 

* cglib 설치하기(cglib를 설치하면 interface를 implement 하지 않아도 실행할 수 있다. 하지만 aop를 사용할 시에는 Interface를 상위에 만들고 service할 객체에서 Interface를 implement 해서 사용하는 것이 좋다.

 

 

 

 

 

Posted by 조은성
,