1 / 31

Advising beans

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

lyn
Download Presentation

Advising beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. www.springframework.co.kr Advising beans 백기선 whiteship2000@gmail.com http://whiteship.tistory.com

  2. www.springframework.co.kr 차례 • AOP 소개 • Classic Spring aspects • Autoproxying • pure-POJO aspects • AspectJ aspects • 요약

  3. www.springframework.co.kr AOP 소개

  4. www.springframework.co.kr AOP 용어 정리 • Advice : 언제 무엇을 할 지 나타냅니다. • Joinpoint : Advice를 적용할 수 있는 지점. • Pointcut : Joinpoint의 부분집합 • Aspect : Advice와 Pointcut을 모아놓은 것 • Weaving : Aspect를 적용하는 작업. • Introduction : 새로운 메소드 또는 필드 추가. • Target : Advice의 대상이 되는 객체. • Proxy : Target에 Advice를 적용한 객체. • Advisor : 하나의 Advice와 하나의 Pointcut을 합쳐놓은 것

  5. www.springframework.co.kr AOP 용어 이해하기Joinpoint

  6. www.springframework.co.kr AOP 용어 이해하기Pointcut

  7. www.springframework.co.kr AOP 용어 이해하기Proxy와 Target

  8. www.springframework.co.kr SpringAOP • 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에서 사용 가능.

  9. www.springframework.co.kr Spring AOP의 특징 • 자바 코드로 작성할 수 있다. • Proxy 객체를 사용한다. • 오직 메소드 실행 Joinpoint만 지원한다.

  10. www.springframework.co.kr Classic Spring aspects • Advice 만들기 • Pointcuts 과 Advisor 정의하기 • ProxyFactoryBean 사용하기

  11. www.springframework.co.kr Classic Spring aspectsAdvice

  12. www.springframework.co.kr Classic Spring aspectsPointcut

  13. www.springframework.co.kr Classic Spring aspectsPointcut • 정규식을 사용하는 Pointcut • org.springframework.aop.support.Perl5RegexpMethodPointcut • org.springframework.aop.support.JdkRegexpMethodPointcut • AspectJ Pointcut 표현식을 사용한 Pointcut • org.springframework.aop.aspectj.AspectJExpressionPointcut

  14. www.springframework.co.kr Classic Spring aspectsAdvisor • Advice + Pointcut • 기본 Advisor • org.springframework.aop.support.DefaultPointcutAdvisor • 정규 표현식 Pointcut을 직접 입력할 수 있는 Advisor • org.springframework.aop.support.RegexpMethodPointcutAdvisor • AspectJ Pointcut을 직접 입력할 수 있는 Advisor • org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor

  15. www.springframework.co.kr ProxyFactoryBean 사용하기

  16. www.springframework.co.kr Classic Spring aspectsProxyFactoryBean • org.springframework.aop.framework.ProxyFactoryBean • target • interceptorNames • proxyInterfaces

  17. www.springframework.co.kr Class Spring aspects 예제 • Aspect 사용 전. • 중복되는 코드 발생. • 단일 책임 원칙 위배. • 코드 가독성 저하. • Aspect 사용 후. • 보다 객체 지향 적인 방법으로 코딩 가능. • 예제 코드 • test/chapter4/ClassicSpringAopTest.java

  18. www.springframework.co.kr ProxyFactoryBean 단점 • Target 마다 하나 씩 ProxyFactoryBean을 만들어 주어야 합니다. • Target bean의 이름을 변경해주어야 합니다. • 하지만 이미 Target은 Advisor 또는 Pointcut을 통해서 알 수 있습니다. • 그렇다면... Proxy를 알아서 만들 수 있지 않을까요?

  19. www.springframework.co.kr Autoproxying • Classic Spring aspects Autoproxying • AutoProxyCreator bean 설정하기 • @AspectJ aspects Autoproxing • <aop:aspectj-autoproxy /> 엘리먼트 등록하기 • BeanPostProcessor 구현체로써, 등록되어 있는 bean들의 정보를 바탕으로 Proxy 객체를 자동으로 생성합니다.

  20. www.springframework.co.kr Classic Spring aspects Autoproxying • BeanNameAutoProxyCreator • beanNames 속성에 설정한 bean들의 Proxy를 생성합니다. • AbstractAdvisorAutoProxyCreator • AspectJAwareAdvisorAutoProxyCreator • @AspectJ 애노테이션이 붙어있는 bean의 Proxy를 생성합니다. • DefaultAdvisorAutoProxyCreator • Spring Container에 등록되어 있는 Advisor들을 바탕으로 Proxy를 생성합니다. • InfrastructureAdvisorAutoProxyCreator • Spring이기본으로 제공하는 Advisor들이 적용되는 Target의 Proxy를 생성합니다. • 테스트 용도도 추정.

  21. www.springframework.co.kr Autoproxying @AspectJ aspects • aop 네임스페이스 추가. • <aop:aspectj-autoproxy />

  22. www.springframework.co.kr Autoproxing 예제 • 기본의 코드에서 ProxyFactoryBean 설정 제거 • DefaultAdvisorAutoProxyCreator bean 설정하기 • @AspectJ aspect 구현 • <aop:aspectj-autoproxy /> 사용하기 • test/AnnotationAspectTest.java

  23. www.springframework.co.kr 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{ …

  24. www.springframework.co.kr POJO Aspect 구현하기 • 별다른 설명이 필요 한가요? • POJO를 Aspect로 사용하는 마술은 bean 설정에서 이루어 집니다. • <aop> 네임스페이스를 공부해야 할 시간이 왔습니다.

  25. www.springframework.co.kr aop 네임스페이스

  26. www.springframework.co.kr POJO Aspect 선언하기

  27. www.springframework.co.kr POJO Aspect 예제 • POJO Aspect 구현(Audience.java) • 구현한 클레스를 bean으로 등록 • aop 네임스페이스로 Aspect 정의 • test/PojoAspectTest.java

  28. www.springframework.co.kr AspectJ • 장점 • 다양한 Joinpoint 지원. • 다양한 Pointcut 표현식 지원. • 단점 • 별도의 문법을 익혀야 함. • 별도의 컴파일 과정이 필요함.

  29. www.springframework.co.kr AspectJ 예제 • AspectJ Asepct 만들기 • bean으로 등록하기 • AJDT로 컴파일하기

  30. www.springframework.co.kr 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.

  31. www.springframework.co.kr 발표 후, 질문 및 Comment • 양철근 : 참 좋았어요. 같은 Advice의 적용 순서. • 최한수 : 졸려~ X 2 • 김계옥 : 갈수록 발표 능력이 좋아져. 설명과 예제의 조화, 졸릴 때 질문 귿. • 심미혜 : 약간 빠르다. 네 이상.

More Related