Spring MVC下对Controller 的AOP切面

本文详细介绍了在Spring MVC中如何使用AOP对Controller进行切面配置,通过WebApplicationContext.xml添加配置,如<aop:pointcut>表达式的解析,以及如何创建并使用aop类如ControllerProxy进行方法拦截和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用spring mvc采用@Controller注解时,对Controller进行Aop拦截不起作用,原因是该注解的Controller已被spring容器内部代理了.
Spring MVC加载的是WebApplicationContext而不是ApplicationContext,问题就在这,应该把schema和加载加到 spring-mvc.xml,也就是

WebApplicationContext.xml中。

1.添加配置如下:

 <aop:config>
<aop:aspect id="serviceAop" ref="aaa">
<aop:pointcut id="servicePointCut"
expression="execution(* com.controller.*.*(..))" />
<aop:around pointcut-ref="servicePointCut" method="doArount" />
</aop:aspect>
</aop:config>
<bean id="aaa" class="com.ControllerProxy">
</bean>



以 <aop:pointcut id="serviceMethod" expression="execution(* *..*Service.*(..))" />为例讲解

首先:这个表达式是分为4块的,即:方法返回类型 包 +(子包)+ 方法名 + 参数个数或者类型

1、第一个 * 表示:对任意的返回类型方法进行匹配

2、第二个 * 表示:  对任意的包并且包的最后是以Service结尾的包

3、第三个 * 表示:  对任意的方法名进行匹配

 4、第四个(..)表示: 通配,即方法中可以有0个或者多个参数,如果想执行参数为2个,即(*, String)表示2个参数,第二个参数为String类型。



2. aop类如下:

public class ControllerProxy {

public Object doArount(ProceedingJoinPoint point) throws Throwable {
// 返回对象
Object ret = null;
// 获得返回对象类型
Signature signature = point.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
String className = methodSignature.getDeclaringType().getSimpleName();
String methodName = method.getName();
       ......
return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值