spring3: Aspectj后置返回通知

Aspectj后置返回通知

接口:

package chapter1.server;

public interface IHelloService2 {
public int sayAfterReturning(String param);
}

  

接口实现

package chapter1.service.impl;

import chapter1.server.IHelloService2;

public class HelloService2 implements IHelloService2 {

	public int sayAfterReturning(String param) {
// TODO Auto-generated method stub
System.out.println("============ say after returning:" + param);
return 1;
} }

  

配置:

一定要加:<aop:aspectj-autoproxy/> 启动对Aspectj的支持

<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService2" />
<bean id="aspect" class="chapter1.aop.HelloAspect2"/>

  

AOP切面:

一定要引入:org.aspectj.lang.annotation.Aspect; 否则不执行

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.AfterReturning; @Aspect
public class HelloAspect2 { //方法一
//通知
@AfterReturning(
//value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",
value="execution(* chapter1..*.sayAfterReturning(..))",
argNames="retVal",
returning="retVal")
public void afterReturningAdvice(Object retVal)
{
System.out.println("================= return after advice : " + retVal);
} //方法二
//定义切入点
@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")
public void returnPointcut(String param) {} public void afterReturningAdvice2(Object retVal)
{ } }

  

测试程序:

@Test
public void testAspectAfterReturning()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");
IHelloService2 hello = context.getBean("helloService", IHelloService2.class);
hello.sayAfterReturning("hahah");
}

  

结果:

============ say after returning:hahah
================= return after advice : 1

上一篇:js实现九九乘法表


下一篇:关于springmvc下服务器文件打包成zip格式下载功能