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
?