如何在sping3.1 MVC中应用aspect注解之AOP

最近在项目有一个操作时间监控功能

项目采用的是 spring3.1 mvc -control注解

为了对所有的操作进行执行时间监控,编写了一个AOP

配置mvc aop的时候,刚开始按照普通的aspect注解的方式配置,无法生效。

后面查了相关的资料,有的说@control不支持AOP ,有的说支持,但笔者最后还是成功的找到了
相关的配置方法,因为笔者喜欢Aspect的注解风格。所以笔者只提供Aspect风格的相关配置。相关jar网上搜下。笔者只讲mvc 切面生效的配置。

前提条件MVC配置成功。

编写AOP类

package com.wx.aop.timer;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Timer {

	private  Logger logger =Logger.getLogger(getClass());
//可以尝试下这两种注解
//      @Around("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))")
	@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public Object logTimer(ProceedingJoinPoint thisJoinPoint) throws Throwable {
		String clazzName = thisJoinPoint.getTarget().getClass().getName();
		String methodName = thisJoinPoint.getSignature().getName();
		
		// 计时并调用目标函数
		long start = System.currentTimeMillis();
		Object result = thisJoinPoint.proceed();
		long time = System.currentTimeMillis() - start;
		 
		// 输出计时信息
		logger.info("操作计时:" + time + "ms  类名: " + clazzName+ "  方法名:" + methodName + "()");

		return result;
	}
}


spring-servlet配置文件加入下面配置语句:

 

<aop:aspectj-autoproxy proxy-target-class="true" />   <!-- aspect注解生效--> 

 <!-- aop切面 -->
 <bean id="timer" class="com.wx.aop.timer.Timer" />

 

笔者mvc配置文件为spring-servlet.xml.整个配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"  
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                  
        http://www.springframework.org/schema/context                   
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop 
             http://www.springframework.org/schema/aop/spring-aop-3.1.xsd     
                   http://www.springframework.org/schema/mvc                
                     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">   
 
 <aop:aspectj-autoproxy proxy-target-class="true" />   <!-- aspect注解生效-->  
 
 <!--  servlet 配置,还需要注册dispathservlet 类 在  web.xml中。具体详情搜索相关的资料 网上很多 -->
 <context:component-scan base-package="com.wx.servlet" />
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>   
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  
 <!-- Default ViewResolver -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp"></property>
   
 </bean>
  
  <!-- aop切面 -->
 <bean id="timer" class="com.wx.aop.timer.Timer" />
</beans>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半部论语

如果觉得有帮助,打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值