Spring AOP日志配置以及注意事项

版权声明:本文为博主anzy原创文章,转载请注明地址 https://blog.csdn.net/qq_24549805/article/details/78706966


一、XML配置方法

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

	<bean id="logAspect" class="com.anzy.frame.interceptor.LogInterceptor">
	</bean>
	<aop:config>
		<aop:aspect ref="logAspect" order="1">
			<aop:pointcut id="logServiceCut" expression="execution (* com.anzy.bussiness.sys.*.*.*.*(..))"/>
			<!-- 方法执行前 -->
			<aop:before pointcut-ref="logServiceCut" method="doBefore" />
			<!-- 方法执行结束后 -->
			<aop:after-returning pointcut-ref="logServiceCut" method="doAfterReturning" returning="rtv"/>
		</aop:aspect>
	</aop:config>

</beans>

package com.anzy.frame.interceptor;

/**
 * Created by anzy on 2017/12/2.
 */

import com.anzy.frame.utils.JsonUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;

import java.util.ArrayList;

//@Aspect
public class LogInterceptor implements Ordered {

    private int order;
    public void setOrder(int order) {
        this.order = order;
    }
    public int getOrder() {
        return order;
    }
    private final Logger logger = LoggerFactory.getLogger(LogInterceptor.class);

//    @Before("execution (* com.anzy.bussiness.sys.*.*.*.*(..))")
    public void doBefore(JoinPoint jp){
//        System.out.print("进入dobefore===============");
        if(logger.isInfoEnabled()){
            String strLog =  jp.getTarget().getClass().getName() + "." + jp.getSignature().getName();
            ArrayList<Object> aList = new ArrayList<Object>();

            for(Object obj : jp.getArgs()){
                if(obj != null && (obj.getClass().getName().startsWith("java.lang") || obj.getClass().getName().startsWith("com.airtrip"))){
                    aList.add(obj);
                }
            }

            logger.info(strLog + "] [开始] [参数:"+ JsonUtil.writeValueAsString(aList));
        }
    }

//    @AfterReturning(value = "execution (* com.anzy.bussiness.sys.service.impl.*.*(..))",  returning="rtv")
    private void doAfterReturning(JoinPoint jp, Object rtv){
        if(logger.isInfoEnabled()){
            String strLog =  jp.getTarget().getClass().getName() + "." + jp.getSignature().getName();
            logger.info(strLog+ "] [结束]"+"[Return:" + rtv + "]");
        }
    }
}


二、注解配置

将上面注释掉的注解开启即可。


三、注意事项:

1、别忘了将配置文件加入web.xml中

2、扫描时一定要注意

springmvc后面还会配置一个扫描,basepackage路径一定要精确到controller否则,可能会造成AOP日志失效。

因为刚开始先扫描了一遍类,将相应的bean加入到了spring管理器中,后来springmvc又扫了一遍,则会覆盖原先的。


同样不仅会造成AOP日志失效,也有可能造成事务的失效。







猜你喜欢

转载自blog.csdn.net/qq_24549805/article/details/78706966
今日推荐