1. AOP 란 무엇입니까?
AOP는 측면 지향 프로그래밍의 약어이며, 이는 측면 지향 프로그래밍을 의미합니다. AOP는 실제로 GOF 디자인 모델의 연속입니다.
2. Spring AOP에 관한 일부 용어 :
A. 측면 : 스프링 AOP에서, 패싯은 일반 클래스를 사용하여 구현하거나 @aspect 주석 (@aspectj style)을 사용한 일반 클래스에서 구현할 수 있습니다.
B. 조인 포인트 : 스프링 AOP에서 연결 지점은 메소드의 실행을 나타냅니다.
C. 조언 : 섹션의 특정 공동 지점에서 수행되는 조치. "주변", "이전"및 "After"를 포함한 다양한 유형의 알림이 있습니다. 스프링을 포함한 많은 AOP 프레임 워크는 인터셉터를 알림 모델로 사용하고 링크 포인트 중심 인터셉터 체인을 유지 관리합니다.
D. POINTCUT :이 메소드를 실행할 때 알림을 생성 할 수있는 하나 또는 메소드 그룹을 정의합니다. Spring은 기본적으로 SpectJ Point-Cut Syntax를 사용합니다.
3. 알림 유형
A. 사전 확인 (@before) : 조인 포인트 전에 실행 된 알림이지만이 알림은 연결 지점 전에 실행을 방해 할 수 없습니다 (예외를 던지지 않는 한)
B. 반환 후 알림 (@AfterReturning) : 연결 지점 (조인 포인트)이 정상적으로 완료된 후 실행 된 알림 : 예를 들어 메소드는 예외를 던지지 않으며 정상적으로 반환됩니다.
C. 예외 후 알림이 발생합니다 (@AfterThrowing) : 메소드가 예외를 던지고 종료 될 때 알림이 실행됩니다.
D. 포스트 알림 (@After) : 연결 지점이 종료 될 때 실행 된 알림 (정상 반환 또는 비정상 출구 여부)
E. 서라운드 알림 (@around) : 메소드 호출과 같은 조인 포인트를 둘러싼 알림. 이것은 가장 강력한 알림 유형이며 서라운드 알림은 메소드 호출 전후에 사용자 정의 동작을 완료 할 수 있습니다. 또한 연결 지점을 계속 실행할 것인지 또는 직접 자신의 반환 값을 반환 할 것인지 또는 실행을 종료하기 위해 예외를 던지는지를 선택합니다.
4. @aspectj 스타일 AOP 구성
Spring AOP 구성에는 두 가지 스타일이 있습니다.
A. XML 스타일 = 선언 형식으로 스프링 AOP 구현
B. 종면 스타일 = 주석 양식을 사용하여 스프링 AOP 구현
5. 예
testaspect
패키지 com.spring.aop; / *** 섹션*/ public class testAspect {public void doafter (joinpoint jp) {system.out.println ( "로그 엔딩 메소드 :" + jp.getTarget (). getClass (). getName () + "." + jp.getSignature (). getName ()); } public Object Doaround (ProceedingJoinPoint PJP) 던지기 가능 {Long Time = System.CurrentTimeMillis (); 객체 retval = pjp.proceed (); time = system.currenttimeMillis () - 시간; System.out.println ( "프로세스 시간 :" + time + "ms"); retval 리턴; } public void dobefore (joinpoint jp) {system.out.println ( "로그 시작 메소드 :" + jp.getTarget (). getClass (). getName () + "." + jp.getSignature (). getName ()); } public void dothrowing (joinpoint jp, trashable ex) {system.out.println ( "method" + jp.gettarget (). getClass (). getName () + "." + jp.getSignature (). System.out.println (ex.getMessage ()); } private void sendex (string ex) {// todo send sms 또는 이메일 알림}}} 패키지 com.spring.service; / ** * 인터페이스 a */ public interface aservice {public void fooa (String _msg); 공개 무효 바라 (); } 패키지 com.spring.service; / ***인터페이스의 구현 클래스 a*/ public class aserviceimpl은 aservice {public void bara () {system.out.println ( "aserviceimpl.bara ()"); } public void fooa (String _msg) {System.out.println ( "aserviceimpl.fooa (msg :"+_ msg+")"); }} 패키지 com.spring.service; / ** * 서비스 클래스 B */ public class bserviceimpl {public void barb (String _msg, int _type) {system.out.println ( "bserviceimpl.barb (msg :"+_ msg+"type :"+_ type+"); if (_type == 1) 새로운 불법 행위 ( "테스트 예외")를 던지십시오. } public void foob () {System.out.println ( "bserviceimpl.foob ()"); }}ApplicationContext
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : aop = "http://www.springframework.org/schema/aop"xsi : schemalocation = "http://www.springframework.org/schema/beans http://ww.springframework.org/schema/beans/spred-beans-2 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "default-autowire ="autodetect "> <aop : config> <aop : inspectppect :"test-bean "> <! com.spring.service 패키지 아래의 인터페이스-> <aop : pointcut id = "businessservice"expression = "execution (*com.spring.service.*.*(..))") "/> <aop : pointcut-Ref ="businessservice "방법 ="dobebe "/> <aop : afterservice ="tother = "tothervice" PointCut-Ref = "BusinessService"Method = "Doaround"/> <aop : After-throwing PointCut-Ref = "BusinessService"메소드 = "Dothrowing"/> </aop : Aspect> </aop : config> <Bean ID = "Sagebean"/> <Bean id = "aservice "> </beans>
테스트 클래스 aoptest
Public Class AopTest는 AbstractDependencyInjectionspringContextTests {private aservice aservice; 개인 bserviceimpl bservice; 보호 된 문자열 [] getConfigLocations () {String [] configs = new String [] { "/applicationContext.xml"}; 반환 구성; } / *** 일반 통화 테스트* / public void testCall () {System.out.println ( "SpringTest Junit Test"); aservice.fooa ( "Junit test fooa"); aservice.bara (); bservice.foob (); bservice.barb ( "Junit test barb", 0); } / ** * Test After-Throwing * / public void testThrow () {try {bservice.barb ( "Junit Call Barb", 1); } catch (delegalargumentexception e) {}} public void setAservice (Aservice Service) {aservice = service; } public void setBeservice (bserviceimpl service) {bservice = service; }}
작업 결과는 다음과 같습니다.
로그 시작 방법 : com.spring.service.aserviceimpl.fooa aserviceimpl.fooa (msg : junit test fooa) 로그 엔딩 메소드 : com.spring.service.aserviceimpl.fooa 프로세스 시간 : 0 MS 로그 시작 방법 : com.spring.serviceimpl.bara aservicepl.bara () 로그 impl.bara () com.spring.service.aserviceimpl.bara 프로세스 시간 : 0 ms log 시작 방법 : com.spring.service.bserviceimpl.foob bserviceimpl.foob () 로그 종료 방법 : com.spring.service.bserviceimpl.foob 프로세스 시간 : com.spring.service.service.service. bserviceimpl.barb (msg : junit test barb type : 0) 로그 엔딩 방법 : com.spring.service.bserviceimpl.barb 프로세스 시간 : 0ms 로그 시작 방법 : com.spring.service.bserviceimpl.barb bserviceimpl.barb (msg : junit call type : 1) 로그 방법 : com.spring.service.bserviceimpl.barb method com.spring.service.bserviceimpl.barb 던지기 예외 테스트 예외