在spring实现
import org.aopalliance.intercept.MethodInvocation;/** * Created by dingshuangkun on 2018/1/9. */public class GreetingAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { before(); Object obj = invocation.proceed(); after(); return obj; } public void before(){ System.out.println("----before----"); } public void after(){ System.out.println("----after------"); }}
greetingAroundAdvice
测试
public static void main(String[] arges){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Greeting greeting =(Greeting) ac.getBean("greetingProxy"); greeting.sayHello("ding"); greeting.sayNiHao("dingding");}
------before-------
hello ding ------after-------- ------before------- niHao dingding ------after--------