<tr id="ieeco"><small id="ieeco"></small></tr>
<acronym id="ieeco"></acronym>

Spring3 aop aspectj 打印日志

applicationContext-aspectj.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:context="http://www.springframework.org/schema/context"
       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.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
       default-lazy-init="true">

<aop:aspectj-autoproxy/>
</beans>

?

在applicationContext.xml?中加載? applicationContext-aspectj.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-lazy-init="true">
	<import resource="spring/applicationContext-aspectj.xml" />	
</beans>

?

?

java代碼

package com.aspectj.aop;
import java.lang.reflect.Method;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect  
@Component 
public class LogInteceptor {
//	private Logger logger = Logger.getLogger(getClass());
	private Logger logger = LoggerFactory.getLogger(LogInteceptor.class);
	
        /**
         * 攔截類的入口
         */
	@Pointcut("execution(* com.content.service.*.*(..))")
	public void pointCut() {
//		logger.info("ponit....");
	}
	
	@Before("pointCut()")
	public void before() {
//		logger.info("被攔截方法調用之前調用此方法,輸出此語句");
	}

	@After("pointCut()")
	public void after() {
//		logger.info("被攔截方法調用之后調用此方法,輸出此語句");
	}
	
	@SuppressWarnings("rawtypes")
	@Around("pointCut()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		String className = pjp.getTarget().getClass().getName(); //攔截類
		String methodName = pjp.getSignature().getName() + "()";
		Object[] paramValues = pjp.getArgs(); //攔截類的入參數

		MethodSignature joinPointObject = (MethodSignature) pjp.getSignature();
		Method method = joinPointObject.getMethod();
		Class[] classTypes = method.getParameterTypes();
		if (logger.isDebugEnabled()) {
			logger.debug("invoke class name: {}, invoke method name: {}, parameter type: {}, parameter value: {}", new Object[] { className, methodName,
					classTypes, paramValues });
		}

		Object obj = pjp.proceed();// 此處返回的是攔截的方法的返回值,如果不執行此方法,則不會執行被攔截的方法,利用環繞通知可以很好的做權限管理
		Object logObj = obj;
		if (obj != null) {
			if (obj instanceof List && ((List) obj).size() > 10) {
				logObj = ((List) obj).size();
			}
		}
		if (logger.isDebugEnabled()) {
			logger.debug("invoke class name: {}, invoke method name: {}, return type: {}, return value: {}",
					new Object[] { className, methodName, method.getReturnType(), logObj });
		}
		return obj;
	}
	
}

?參照 http://lipsion.iteye.com/blog/1597930

?參照 http://tuoxinquyu.iteye.com/blog/1465187

?

相關推薦
??2020 CSDN 皮膚主題: 大白 設計師:CSDN官方博客 返回首頁
彩票送彩金