把前置通知和后置通知都写到一个通知中,组成了环绕通知
新建类,实现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