@AspectJ采用注解描述

 

切点表达式函数:

execution(* greetTo(..))切点表达式

execution:代表执行某一个方法

* greetTo(..):为操作数描述目标方法的匹配串

 

1 public interface Waiter1 {
2     void greetTo(String name);
3     void serveTo(String name);
4 }

 

 1 package com.asm;
 2 
 3 public class NavieWaiter implements Waiter1 {
 4 
 5     public void greetTo(String name) {
 6         System.out.println("waiter greet to " + name + "...");
 7     }
 8 
 9     public void serveTo(String name) {
10         System.out.println("waiter serving " + name + "...");
11     }
12 
13 }

 

 

 1 //通过注解标识为切面
 2 @Aspect
 3 public class PreGreetingAspect {
 4     
 5     //定义切点和增强类型
 6     @Before("execution(* greetTo(..))")
 7     public void beforeGreeting(){
 8         System.out.println("How are you");
 9     }
10 }

 

 1 public class AspectJProxyTest {
 2     
 3     public static void main(String[] args) {
 4         
 5         Waiter1 target = new NavieWaiter();
 6         AspectJProxyFactory factory = new AspectJProxyFactory();
 7         factory.setTarget(target);
 8         factory.addAspect(PreGreetingAspect.class);
 9         Waiter1 proxy = factory.getProxy();
10         proxy.greetTo("john");
11         proxy.serveTo("john");
12     }
13 }

 

posted on 2016-06-20 17:35  Sharpest  阅读(234)  评论(0编辑  收藏  举报