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

发布网友 发布时间:2022-04-25 20:41

我来回答

2个回答

热心网友 时间:2022-04-12 21:04

最近在项目有一个操作时间监控功能
项目采用的是 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>

转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦

热心网友 时间:2022-04-12 22:22

编写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>

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com