aop所需jar包
asm.jar
asm-attrs.jar
asm-commons-2.2.3.jar
asm-util-2.2.3.jar
aspectjlib.jar
aspectjrt.jar
aspectjweaver.jar
Demo
===============dao层=============
package org.dao.interfaces;
/**
*@author:张
*2009-6-10 下午08:35:19
*/
public interface IUser {
public void save(String userName);
}
package org.dao;
import org.dao.interfaces.IUser;
import org.springframework.stereotype.Repository;
/**
*@author:张
*2009-6-10 下午08:38:12
*/
@Repository
public class UserImpl implements IUser {
public void save(String userName) {
System.out.println("用户"+userName+"保存成功!");
}
}
==========================service层=============================
package org.dao;
import org.dao.interfaces.IUser;
import org.springframework.stereotype.Repository;
/**
*@author:张奇峰
*2009-6-10 下午08:38:12
*/
@Repository
public class UserImpl implements IUser {
public void save(String userName) {
System.out.println("用户"+userName+"保存成功!");
}
}
==============切面================================
package org.aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
*@author:张奇峰
*2009-6-24 下午08:36:46
*/
//设置为一个切面
@Aspect
@Component
public class AopDemoClass {
//切点表达式
/**第一个* 表示切点返回值得类型 *表示任何类型
* ..*表示拦截org.service包下的类及其子包下的类 如要拦截某类可直接.ClassName
*后面的.*要拦截的方法
*(..)方法的参数 此表示任何参数
*/
@Pointcut("execution(* org.service.UserService.*(..))")
public void anyMethod() //切点名称
{}
@Before("anyMethod()")
public void beforeAdvice()
{
System.out.println("执行前置通知!");
}
@AfterReturning("anyMethod()")//名称也要加括号
public void afterAdvice()
{
System.out.println("执行后置通知!");
}
@Around("anyMethod()")
public Object doAction(ProceedingJoinPoint pjp)throws Throwable
{
System.out.println("环绕执行开始");
Object resultObject=pjp.proceed();//这个方法就是执行业务方法 一般都要执行这个方法
System.out.println("环绕执行完毕");
return resultObject;
}
}
======================beans.xml===========================
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<context:component-scan base-package="org"/>
<context:annotation-config/>
<aop:aspectj-autoproxy/>
</beans>
==========================TEST============================
package org.test;
import org.service.UserService;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*@author:张奇峰
*2009-6-24 下午08:41:43
*/
public class AopTestClass {
@org.junit.Test
public void Test()
{
AbstractApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
UserService service=(UserService)context.getBean("userService");
service.save("zhanqsan");
context.close();
}
}
输出:
执行前置通知!
环绕执行开始
用户zhanqsan保存成功!
执行后置通知!
环绕执行完毕