spring要实现aop需要:aspectjweaver.jar , aspectjrt.jar , cglib-nodep.jar
实现注解:common-annotations.jar
用注解方式实现的切面类:
package com.fsj.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* @Aspect 定义为切面
* @Component 自动扫描的组件,把此类交给spring管理
*
*/
@Component @Aspect
public class MyIntercepor {
/**
* @Pointcut 定义为切入点,切入点的名称即为方法名(包含括号)
* execution(* com.fsj..*.*(..))说明:
* 第一个“*”表示所有返回值类型,
* fsj后面的两个点表示包含子包,
* 第二个“*”表示所有类,
* 第三个“*”表示所有方法
* 括号中的两点表示任意个参数任意参数类型
*
@Pointcut("execution(* com.fsj..*.*(..))")
public void anyMethod()
{
}
*/
/**
* 只拦截com.fsj.service.impl.PersonServiceBean中的所有方法
*/
@Pointcut("execution(* com.fsj.service.impl.PersonServiceBean.*(..))")
public void anyMethod()
{
}
/**
* 定义前置通知
* 括号中为切入点的名称
*/
@Before("anyMethod()")
public void doAccessCheck()
{
System.out.println("-------1.前置通知--------");
}
/**
* 定义后置通知
* 括号中为切入点的名称
*/
@AfterReturning("anyMethod()")
public void doAfterReturning()
{
System.out.println("-------5.后置通知--------");
}
/**
* 定义最终通知
* 括号中为切入点的名称
*/
@After("anyMethod()")
public void doAfter()
{
System.out.println("-------3.最终通知--------");
}
/**
* 定义最终通知
* 括号中为切入点的名称
*/
@AfterThrowing("anyMethod()")
public void doAfterThrowing()
{
//异常通知在最终通知后执行
//有异常时不会执行后置通知,也不会有环绕后通知
System.out.println("-------异常通知--------");
}
/**
* 定义环绕通知
* 括号中为切入点的名称
* 环绕通知特别适合做权限控制
*/
@Around("anyMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable
{
Object result = null;
System.out.println("-------2.环绕前通知:进入方法--------");
result = pjp.proceed();
System.out.println("-------4.环绕后通知:退出方法--------");
return result;
}
}
配置文件是:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <aop:aspectj-autoproxy /><!-- 启用aop --> <context:component-scan base-package="com.fsj" /><!-- 启用自动扫描和管理bean --> <!-- <bean id="myIntercepor" class="com.fsj.aop.MyIntercepor"></bean> <aop:config> <aop:aspect id="asp" ref="myIntercepor"> <aop:pointcut id="mycut" expression="execution(* com.fsj.service.impl.PersonServiceBean.*(..))"/> <aop:before method="doAccessCheck" pointcut-ref="mycut" /> <aop:after-returning method="doAfterReturning" pointcut-ref="mycut"/> <aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut" /> <aop:after method="doAfter" pointcut-ref="mycut" /> <aop:around method="doAround" pointcut-ref="mycut" /> </aop:aspect> </aop:config> xml形式做aop --> </beans>
还可以得到拦截的参数和返回值:
/**
* 只拦截com.fsj.dao.impl.PersonDaoImpl中的所有方法
*/
@Pointcut("execution(* com.fsj.dao.impl.PersonDaoImpl.*(..))")
public void anyMethod()
{
}
/**
* 拦截anyMethod()指定的方法并且要满足只有一个int类型的参数的方法
* 得到输入参数
* @param id
*/
@Before("anyMethod() && args(id)")
public void doAccessCheck(int id)
{
System.out.println("-------前置通知--------" + id);
}
/**
* 拦截anyMethod()指定的方法
* 并且要满足只有一个String类型的参数和一个boolean类型的返回值的方法
* @param name 输入参数
* @param result 返回值
*/
@AfterReturning(pointcut="anyMethod() && args(name)",returning="result")
public void doAfterReturning(String name,boolean result)
{
//有异常时不会执行后置通知
System.out.println("-------后置通知--------name:"+name+"----------返回值:"+result);
}
@After("anyMethod()")
public void doAfter()
{
System.out.println("-------最终通知--------");
}
/**
* 拦截异常并获得异常信息
* @param e
*/
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e)
{
System.out.println("-------异常通知--------"+e);
}
或者把连接点作为参数传入通知:
@Aspect
public class TestAspect {
@Pointcut("execution(* *(..))")
private void pointCutMethod(){};
@After("pointCutMethod()")
// @After("execution(* *(String))")
public void doAfter(JoinPoint jp) {
System.out.print("日志记录(方法执行后):\t"
+ "类名:" + jp.getTarget().getClass().getName() + "方法名:"
+ jp.getSignature().getName() + "\t");
Object[] args = jp.getArgs();
System.out.print("参数:" );
for(Object obj : args){
System.out.print(" " + obj + " ");
}
System.out.println();
}
@Around("execution(* *(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("方法执行时间: " + time + " ms");
return retVal;
}
@Before("execution(* *(..))")
public void doBefore(JoinPoint jp) {
System.out.println("日志记录(方法执行之前): "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}
@AfterThrowing(pointcut="pointCutMethod()" , throwing="e")
public void doThrowing(JoinPoint jp, Throwable e) {
System.out.println("method " + jp.getTarget().getClass().getName()
+ "." + jp.getSignature().getName() + " throw exception");
System.out.println(e.getMessage());
this.sendEx(e.getMessage());
}
private void sendEx(String ex) {
//TODO 发送短信或邮件提醒
if(ex != null)
System.out.println("向管理员发送警告");
}
54万+

被折叠的 条评论
为什么被折叠?



