advising beans

31
Spring In Action 2nd Advising beans 백백백 [email protected] http:// whiteship.tistory.com www.springframework.co.kr

Upload: lyn

Post on 21-Jan-2016

100 views

Category:

Documents


0 download

DESCRIPTION

Advising beans. 백기선 [email protected] http://whiteship.tistory.com. 차례. AOP 소개 Classic Spring aspects Autoproxying pure-POJO aspects AspectJ aspects 요약. AOP 소개. AOP 용어 정리. Advice : 언제 무엇을 할 지 나타냅니다 . Joinpoint : Advice 를 적용할 수 있는 지점 . Pointcut : Joinpoint 의 부분집합 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advising beans

Spring In Action 2nd

Advising beans

백기선[email protected]://whiteship.tistory.com

www.springframework.co.kr

Page 2: Advising beans

Spring In Action 2nd

차례

• AOP 소개• Classic Spring aspects• Autoproxying• pure-POJO aspects• AspectJ aspects• 요약

www.springframework.co.kr

Page 3: Advising beans

Spring In Action 2nd

AOP 소개

www.springframework.co.kr

Page 4: Advising beans

Spring In Action 2nd

AOP 용어 정리

• Advice : 언제 무엇을 할 지 나타냅니다 .• Joinpoint : Advice 를 적용할 수 있는 지점 .• Pointcut : Joinpoint 의 부분집합• Aspect : Advice 와 Pointcut 을 모아놓은 것• Weaving : Aspect 를 적용하는 작업 .

• Introduction : 새로운 메소드 또는 필드 추가 .• Target : Advice 의 대상이 되는 객체 .• Proxy : Target 에 Advice 를 적용한 객체 .• Advisor : 하나의 Advice 와 하나의 Pointcut 을

합쳐놓은 것

www.springframework.co.kr

Page 5: Advising beans

Spring In Action 2nd

AOP 용어 이해하기Joinpoint

www.springframework.co.kr

Page 6: Advising beans

Spring In Action 2nd

AOP 용어 이해하기Pointcut

www.springframework.co.kr

Page 7: Advising beans

Spring In Action 2nd

AOP 용어 이해하기Proxy 와 Target

www.springframework.co.kr

Page 8: Advising beans

Spring In Action 2nd

Spring AOP

• Classic Spring proxy-based AOP – 모든 버전의 Spring 에서 사용 가능 .

• @AspectJ annotation-driven aspects– Spring 2.0, Java 5 이상에서 사용 가능 .

• Pure-POJO aspects– Spring 2.0 이상에서 사용 가능 .

• Injected AspectJ aspects– 모든 버전의 Spring 에서 사용 가능 .

www.springframework.co.kr

Page 9: Advising beans

Spring In Action 2nd

Spring AOP 의 특징

• 자바 코드로 작성할 수 있다 .• Proxy 객체를 사용한다 .• 오직 메소드 실행 Joinpoint 만

지원한다 .

www.springframework.co.kr

Page 10: Advising beans

Spring In Action 2nd

Classic Spring aspects

• Advice 만들기• Pointcuts 과 Advisor 정의하기• ProxyFactoryBean 사용하기

www.springframework.co.kr

Page 11: Advising beans

Spring In Action 2nd

Classic Spring aspectsAdvice

www.springframework.co.kr

Page 12: Advising beans

Spring In Action 2nd

Classic Spring aspectsPointcut

www.springframework.co.kr

Page 13: Advising beans

Spring In Action 2nd

Classic Spring aspectsPointcut

• 정규식을 사용하는 Pointcut– org.springframework.aop.support.Perl5R

egexpMethodPointcut– org.springframework.aop.support.JdkReg

expMethodPointcut

• AspectJ Pointcut 표현식을 사용한 Pointcut– org.springframework.aop.aspectj.AspectJ

ExpressionPointcut

www.springframework.co.kr

Page 14: Advising beans

Spring In Action 2nd

Classic Spring aspectsAdvisor• Advice + Pointcut• 기본 Advisor

– org.springframework.aop.support.DefaultPointcutAdvisor

• 정규 표현식 Pointcut 을 직접 입력할 수 있는 Advisor– org.springframework.aop.support.RegexpMethodPoin

tcutAdvisor• AspectJ Pointcut 을 직접 입력할 수 있는 Advisor

– org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor

www.springframework.co.kr

Page 15: Advising beans

Spring In Action 2nd

ProxyFactoryBean 사용하기

www.springframework.co.kr

Page 16: Advising beans

Spring In Action 2nd

Classic Spring aspectsProxyFactoryBean

• org.springframework.aop.framework.ProxyFactoryBean– target– interceptorNames– proxyInterfaces

www.springframework.co.kr

Page 17: Advising beans

Spring In Action 2nd

Class Spring aspects 예제

• Aspect 사용 전 .– 중복되는 코드 발생 .– 단일 책임 원칙 위배 .– 코드 가독성 저하 .

• Aspect 사용 후 .– 보다 객체 지향 적인 방법으로 코딩 가능 .

• 예제 코드– test/chapter4/ClassicSpringAopTest.java

www.springframework.co.kr

Page 18: Advising beans

Spring In Action 2nd

ProxyFactoryBean 단점

• Target 마다 하나 씩 ProxyFactoryBean을 만들어 주어야 합니다 .

• Target bean 의 이름을 변경해주어야 합니다 .

• 하지만 이미 Target 은 Advisor 또는 Pointcut 을 통해서 알 수 있습니다 .– 그렇다면 ... Proxy 를 알아서 만들 수 있지

않을까요 ?

www.springframework.co.kr

Page 19: Advising beans

Spring In Action 2nd

Autoproxying

• Classic Spring aspects Autoproxying– AutoProxyCreator bean 설정하기

• @AspectJ aspects Autoproxing– <aop:aspectj-autoproxy /> 엘리먼트 등록하기

• BeanPostProcessor 구현체로써 , 등록되어 있는 bean 들의 정보를 바탕으로 Proxy 객체를 자동으로 생성합니다 .

www.springframework.co.kr

Page 20: Advising beans

Spring In Action 2nd

Classic Spring aspects Autoproxying• BeanNameAutoProxyCreator

– beanNames 속성에 설정한 bean 들의 Proxy 를 생성합니다 .

• AbstractAdvisorAutoProxyCreator– AspectJAwareAdvisorAutoProxyCreator

• @AspectJ 애노테이션이 붙어있는 bean 의 Proxy 를 생성합니다 .

– DefaultAdvisorAutoProxyCreator• Spring Container 에 등록되어 있는 Advisor 들을 바탕으로

Proxy 를 생성합니다 .– InfrastructureAdvisorAutoProxyCreator

• Spring 이 기본으로 제공하는 Advisor 들이 적용되는 Target 의 Proxy 를 생성합니다 .

• 테스트 용도도 추정 .

www.springframework.co.kr

Page 21: Advising beans

Spring In Action 2nd

Autoproxying @AspectJ aspects

• aop 네임스페이스 추가 .

• <aop:aspectj-autoproxy />

www.springframework.co.kr

Page 22: Advising beans

Spring In Action 2nd

Autoproxing 예제

• 기본의 코드에서 ProxyFactoryBean 설정 제거

• DefaultAdvisorAutoProxyCreator bean 설정하기

• @AspectJ aspect 구현• <aop:aspectj-autoproxy /> 사용하기

• test/AnnotationAspectTest.java

www.springframework.co.kr

Page 23: Advising beans

Spring In Action 2nd

Declaring pure-POJO aspects

• POJO 는– 특정 ( 규약 역할을 하는 ) 클래스를 상속받으면 안

된다 .• public class Foo extends

javax.servlet.http.HttpServlet{ …– 특정 ( 규약 역할을 하는 ) 인터페이스를 구현하면

안 된다 .• public class Bar implements javax.ejb.EntityBean{ …

– 특정 ( 규약 역할을 하는 ) 애노테이션을 붙이면 안 된다 .

• @javax.ejb.Entitypublic class Baz{ …

www.springframework.co.kr

Page 24: Advising beans

Spring In Action 2nd

POJO Aspect 구현하기

• 별다른 설명이 필요 한가요 ?• POJO 를 Aspect 로 사용하는 마술은

bean 설정에서 이루어 집니다 .• <aop> 네임스페이스를 공부해야 할

시간이 왔습니다 .

www.springframework.co.kr

Page 25: Advising beans

Spring In Action 2nd

aop 네임스페이스

www.springframework.co.kr

Page 26: Advising beans

Spring In Action 2nd

POJO Aspect 선언하기

www.springframework.co.kr

Page 27: Advising beans

Spring In Action 2nd

POJO Aspect 예제

• POJO Aspect 구현 (Audience.java)• 구현한 클레스를 bean 으로 등록• aop 네임스페이스로 Aspect 정의

• test/PojoAspectTest.java

www.springframework.co.kr

Page 28: Advising beans

Spring In Action 2nd

AspectJ

• 장점– 다양한 Joinpoint 지원 .– 다양한 Pointcut 표현식 지원 .

• 단점– 별도의 문법을 익혀야 함 .– 별도의 컴파일 과정이 필요함 .

www.springframework.co.kr

Page 29: Advising beans

Spring In Action 2nd

AspectJ 예제

• AspectJ Asepct 만들기• bean 으로 등록하기• AJDT 로 컴파일하기

www.springframework.co.kr

Page 30: Advising beans

Spring In Action 2nd

Summary

• AOP 는 객체 지향 프로그래밍을 보완하는 강력한 도구이다 .

• Spring provides an AOP framework that lets you insert aspects around method executions.

• You have several choices in how you can use aspects in your Spring applications.

• Finally, there are times when Spring AOP isn’t powerful enough and you must turn to AspectJ for more powerful aspects.

www.springframework.co.kr

Page 31: Advising beans

Spring In Action 2nd

발표 후 , 질문 및 Comment

• 양철근 : 참 좋았어요 . 같은 Advice의 적용 순서 .

• 최한수 : 졸려 ~ X 2• 김계옥 : 갈수록 발표 능력이 좋아져 .

설명과 예제의 조화 , 졸릴 때 질문 귿 .• 심미혜 : 약간 빠르다 . 네 이상 .

www.springframework.co.kr