1、首先是配置文件:

上图是让aop配置正确,不报红;

完整代码:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <description>Spring MVC Configuration</description> <!-- 启用spring mvc 注解 -->
<context:annotation-config/> <!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.credi****mony.adapter"></context:component-scan> <!-- 完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties"/>
<context:property-placeholder location="classpath:log4j.properties"/>
<!-- UTF8解决乱码问题 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean> <mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
<!-- 定义视图文件解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${web.view.prefix}"/>
<property name="suffix" value="${web.view.suffix}"/>
</bean> <!-- 对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 -->
<mvc:default-servlet-handler/> <!-- 静态资源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/> <!-- 定义无Controller的path<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:${web.view.index}"/> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop>
<prop key="java.lang.Throwable">error/500</prop>
</props>
</property>
</bean> <!-- 开启自动切面代理 -->
<aop:aspectj-autoproxy/>
</beans>

  2、对应的class文件,exection里的表达式要正确:

package com.creditharmony.adapter.service.baffle;

import com.creditharmony.common.util.DateUtils;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; import java.util.Date; @Aspect
@Component
public class InterceptorBaffle {
private static final Logger logger = Logger.getLogger(InterceptorBaffle.class); // 一分钟,即60000ms
private static final long ONE_MINUTE = 60000; // service层的统计耗时切面,类型必须为final String类型的,注解里要使用的变量只能是静态常量类型的
public static final String POINT = "execution (* com.cred####ony.adapter.service..*.*(..))"; /**
* 进入方法后打印日志
* @param joinPoint
*/
@Before(POINT)
public void before(JoinPoint joinPoint) {
logger.debug(this.getMethodName(joinPoint)+" start "+ DateUtils.formatDateTime(new Date()));
} /**
* 方法结束打印日志
* @param joinPoint
*/
@After(POINT)
public void after(JoinPoint joinPoint) {
logger.debug(this.getMethodName(joinPoint)+" after"+ DateUtils.formatDateTime(new Date()));
} /**
* 统计方法执行耗时Around环绕通知
* @param joinPoint
* @return
*/
@Around(POINT)
public Object timeAround(ProceedingJoinPoint joinPoint) {
// 定义返回对象、得到方法需要的参数
Object obj = null;
Object[] args = joinPoint.getArgs();
long startTime = System.currentTimeMillis(); try {
obj = joinPoint.proceed(args);
} catch (Throwable e) {
logger.error("统计某方法执行耗时环绕通知出错", e);
} // 获取执行的方法名
long endTime = System.currentTimeMillis();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); // 打印耗时的信息
this.printExecTime(methodName, startTime, endTime); return obj;
} /**
* 打印方法执行耗时的信息,如果超过了一定的时间,才打印
* @param methodName
* @param startTime
* @param endTime
*/
private void printExecTime(String methodName, long startTime, long endTime) {
long diffTime = endTime - startTime;
if (diffTime > ONE_MINUTE) {
logger.warn("-----" + methodName + " 方法执行耗时:" + diffTime + " ms");
}
} /**
* 获取方法名(类的详细包路径)
* @param joinPoint
* @return
*/
private String getMethodName(JoinPoint joinPoint){
return joinPoint.getSignature().getDeclaringTypeName() +
"." + joinPoint.getSignature().getName();
} }

  

最新文章

  1. 控制window.print不打印某些内容
  2. DBCC Check
  3. easyui 很好很强大
  4. 如何通过js获取iframe框架中的内容
  5. react native 添加第三方插件react-native-orientation(横竖屏设置功能 android)
  6. Lambda应用场景和使用实例
  7. Oracle打印日历功能
  8. 使用Sublime Text 或 vs2017开发Node.js程序
  9. 对弈的C++学习笔记
  10. 报错Unexpected token u
  11. Lua论分析需求(学好英文)的重要性
  12. webstrom左侧项目栏不显示文件夹问题
  13. 滑块视图容器 &#160; swiper
  14. js 根据对象属性对数组进行按字母排序
  15. Jmeter目录文件讲解
  16. 使用 sqlyog 导入导出数据显示 lost connection to mysql server during query
  17. MySQL &#183; 关系模型的基本术语
  18. 〖Linux〗Debian 7.1.0 Wheezy使用ltib报错的解决办法
  19. nyoj1237 最大岛屿(河南省第八届acm程序设计大赛)
  20. ptyhon技能树及其学习资源

热门文章

  1. MySQL针对Swap分区的运维注意点
  2. 第二次作业 对VC++6.0编译软件的评价
  3. wordcount程序
  4. 软工个人博客作业Week 1
  5. idea使用优化
  6. Leetcode——50.Pow(x, n)
  7. Doors Breaking and Repairing CodeForces - 1102C (思维)
  8. Alpha冲刺随笔集
  9. Eclipse布局问题小记
  10. [转帖]GitHub上整理的一些工具