1. 程式人生 > >環繞通知(Schema-based方式)

環繞通知(Schema-based方式)

把前置通知和後置通知都寫到一個通知中,組成了環繞通知

新建類,實現MethodInterceptor(方法攔截器)介面

public class MyAround implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {
		System.out.println("11111111111");
		Object result = arg0.proceed();
		System.out.println("22222222222");
		return result;
	}

}

xml

  <bean id="myaround" class="com.lee.advice.MyAround"/>
  <aop:config>  
   		<aop:pointcut expression="execution(* com.lee.service.*.*(..))" id="mypoint"/>
   		<aop:advisor advice-ref="myaround" pointcut-ref="mypoint"/>
   </aop:config>

切點

public class DemoService {
	public void demoMethod() {
		System.out.println("DemoMethod execute");
	}
}

測試程式碼在這裡插入程式碼片


public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		DemoService ds = (DemoService) ac.getBean("demo");
		ds.demoMethod();
	}
}

輸出

11111111111
DemoMethod execute
22222222222