Spring 기초: AOP 프로 그래 밍 (5)

4949 단어 spring
더 읽 기
Schema 기반 AOP 프로 그래 밍
AspectJ 를 바탕 으로 하 는 AOP 프로 그래 밍 은 우리 의 프로 그래 밍 수 요 를 만족 시 킬 수 있 습 니 다. 왜 여기 서 Schema 를 바탕 으로 하 는 논 리 를 해 야 합 니까? 여기 에는 두 가지 이유 가 있 습 니 다.
1. 자바 언어 는 5.0 에 이 르 러 서 야 주해 기능 을 지원 하기 때문에 5.0 이전 버 전에 서도 AspectJ 의 편 의 를 체험 하려 면 특별한 방법 이 필요 하 다.
2. AspectJ 는 절단면 Advisor 를 대상 으로 프로 그래 밍 할 수 없 지만 Schema 는 가능 합 니 다.
선행 강화:

public class AdviceMethods {
    public void preGreeting() {
        System.out.println("--how are you--");
    }
}






    
        
        
    


테스트 코드:

public class SchemaProxyTest {
    public static void main(String[] args) {
        String configLocation = "com/firethewhole/maventest08/schema/beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
        Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
        Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter");
        naiveWaiter.greetTo("John");
        naughtyWaiter.greetTo("John");
    }
}

출력:
--how are you-- NaiveWaiter.greet to John NaughtyWaiter: greet to John
저 희 는 주석 이나 AspectJ 와 관련 된 기능 을 사용 하지 않 았 습 니 다. XML 설정 류 와 관련 된 Bean 과 도 같은 효 과 를 거 두 었 습 니 다.
후방 증강:



설정 파일 에 반환 값 을 연결 할 수 있 습 니 다.
서 라운드 강화:



서 라운드 강화 연결 점 정보 연결 가능

public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("      :" + pjp.getTarget().getClass());
    System.out.println("   :" + pjp.getArgs()[0]);
    pjp.proceed();
    System.out.println("      :" + pjp.getTarget().getClass());
}

이상 증강 던 지기:



public void afterThrowingMethod(IllegalArgumentException iae) {
    System.out.println("    :" + iae.getMessage());
}

여기 귀속 클래스 이상 정보 입 니 다.
최종 강화:



이상 이 있 든 없 든 집행 할 것 이다.
소개 강화:



바 인 딩 매개 변수 강화:



public void bindParams(int num, String name) {
    System.out.println("-----bindParams-----");
    System.out.println("name:" + name);
    System.out.println("num:" + num);
    System.out.println("-----bindParams-----");
}

public class SchemaProxyTest {
    public static void main(String[] args) {
        String configLocation = "com/firethewhole/maventest08/schema/beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
        Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
        ((NaiveWaiter)naiveWaiter).smile("John", 2);
    }
}

출력:
-----bindParams----- name:John num:2 -----bindParams----- NaiveWaiter.smile to John 2 times
  • maventest08.zip (59.7 KB)
  • 다운로드 횟수: 0
  • 좋은 웹페이지 즐겨찾기