Spring3.0中的AOP配置方法

Spring3.0中的AOP配置方法,第1张

第一种配置方法 使用@AspectJ标签

在配置文件中添加注解

创建一个Java文件 使用@Aspect注解修饰该类

创建一个方法 使用@Before @After @Around等进行修饰 在注解中写上切入点的表达式

说明 上述Java文件创建好后 需要将其在Spring的容器中进行声明 可以在配置文件中定义节点 也可以使用@Component组件进行修饰

示例

Java代码

import aspectj lang ProceedingJoinPoint

import aspectj lang annotation After

import aspectj lang annotation AfterThrowing

import aspectj lang annotation Around

import aspectj lang annotation Aspect

import aspectj lang annotation Before

import springframework stereotype Component

/**

* 基于注解的AOP日志示例

* @author ZYWANG

*/

@Component

@Aspect

public class AopLog {

//方法执行前调用

@Before( execution (* zywang services impl * *( )) )

public void before() {

System out println( before )

}

//方法执行后调用

@After( execution (* zywang services impl * *( )) )

public void after() {

System out println( after )

}

//方法执行的前后调用镇历

@Around( execution (* zywang services impl * *( )) )

public Object around(ProceedingJoinPoint point) throws Throwable{

System out println( begin around )

Object object = point proceed()

System out println( end around )

return object

}

//方法运行出现异常时调用

@AfterThrowing(pointcut = execution (* zywang services impl * *( )) throwing = ex )

public void afterThrowing(Exception ex){

System out println( afterThrowing )

System out println(ex)

}

}

上如宴面这段代码中多次使用了重复的切入点 这种情况下 可以使用@Pointcut标注 来修改一个切入点方法(这个方法不需要参数和方法体) 然后就可以在@Before等标注中引用该方法作为切入点 示例如下

Java代码

import aspectj lang ProceedingJoinPoint

import aspectj lang annotation Around

import aspectj lang annotation Aspect

import aspectj lang annotation Before

import aspectj lang annotation Pointcut

import springframework stereotype Component

/**

* 基于注解的AOP日志示渣旅银例

* @author ZYWANG

*/

@Component

@Aspect

public class AopLog {

@Pointcut( execution (* iflysse school services impl * *( )) )

public void pointcut(){}

//方法执行前调用

@Before( pointcut() )

public void before() {

System out println( before )

}

//方法执行的前后调用

@Around( pointcut() )

public Object around(ProceedingJoinPoint point) throws Throwable{

System out println( begin around )

Object object = point proceed()

System out println( end around )

return object

}

}

第二种配置方法 基于配置文件的配置

创建一个Java文件 并指定一个用于执行拦截的方法 该方法可以有 个或多个参数

在Spring配置文件中注册该Java类为一个Bean

使用 等标签进行配置

示例

Java文件

Java代码

import aspectj lang ProceedingJoinPoint

/**

* 基于配置文件的AOP日志示例

* @author ZYWANG

*/

public class AopLog {

//方法执行的前后调用

public Object runOnAround(ProceedingJoinPoint point) throws Throwable{

System out println( begin around )

Object object = point proceed()

System out println( end around )

return object

}

}

Spring配置文件

Xml代码

注意 上面这个示例使用的是around方式的拦截 该方法要求Java类中的方法有一个ProceedingJoinPoint类型的参数

lishixinzhi/Article/program/Java/ky/201311/28907

配置通知时需实现 org.springframework.aop 包下的一些接口

前置通知:MethodBeforeAdvice

后置通知:AfterReturningAdvice

环绕通知:MethodInterceptor

异常通知:ThrowsAdvice

创建被代理对象

通知(Advice)

切入点(Pointcut):通过正则表达式描述指定切入模仿点(某些 指定方法)

Advisor(高级通知) = Advice(通知) + Pointcut(切入点)

创建自动代锋则理

导入Aspectj相关依赖

通知方法名随便起,没有限制

使用Aspectj实旦基纤现切面,使用Spring AOP进行配置


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12278872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存