<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- aop -->
<bean id="logService" class="cn.com.wavenet.core.factory.aspectJ.SysLogAspect"></bean>
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
package cn.com.wavenet.core.factory.aspectJ;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)//注解会在class中存在,运行时可通过反射获取
@Target(ElementType.METHOD)//目标是方法
@Documented//文档生成时,该注解将被包含在javadoc中,可去掉
public @interface OperationLogger
{
/**
* 模块名字
*/
String modelName() default "";
/**
* 操作类型
*/
String option();
}
package cn.com.wavenet.core.factory.aspectJ;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Logable
{
private Logger logger = Logger.getLogger(this.getClass());
/**
* 日志info级别
*
* @param message
*/
public void logInfo(Object message)
{
if (logger.isInfoEnabled())
{
logger.info(message);
}
}
/**
* 日志info级别
*
* @param message
* @param t
*/
public void logInfo(Object message, Throwable t)
{
if (logger.isInfoEnabled())
{
logger.info(message, t);
}
}
/**
* 日志warn级别
*
* @param message
*/
public void logWarn(Object message)
{
logger.warn(message);
}
/**
* 日志warn级别
*
* @param message
* @param t
*/
public void logWarn(Object message, Throwable t)
{
if(logger.isEnabledFor(Level.WARN))
{
logger.warn(message, t);
}
}
/**
* 日志trace级别
*
* @param message
*/
public void logTrace(Object message)
{
if (logger.isTraceEnabled())
{
logger.trace(message);
}
}
/**
* 日志trace级别
*
* @param message
* @param t
*/
public void logTrace(Object message, Throwable t)
{
if (logger.isTraceEnabled())
{
logger.trace(message, t);
}
}
/**
* 方法描述:日志error级别
*
* @param message
*/
public void logError(Object message)
{
if(logger.isEnabledFor(Level.ERROR))
{
logger.error(message);
}
}
/**
* 日志trace级别
*
* @param message
* @param t
*/
public void logError(Object message, Throwable t)
{
if(logger.isEnabledFor(Level.ERROR))
{
logger.error(message);
}
}
}
package cn.com.wavenet.core.factory.aspectJ;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class SysLogAspect
{
private static final Logger logger = Logger.getLogger(SysLogAspect.class);
/**
* 定义Pointcut,Pointcut的名称,此方法不能有返回值,该方法只是一个标示
*/
@Pointcut("@annotation(cn.com.wavenet.core.factory.aspectJ.OperationLogger)")
public void controllerAspect()
{
System.out.println("我是一个切入点");
}
/**
* 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。
* @param joinPoint
*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint)
{
System.out.println("=====SysLogAspect前置通知开始=====");
//handleLog(joinPoint, null);
}
/**
* 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
* @param joinPoint
*/
@AfterReturning(pointcut = "controllerAspect()")
public void doAfter(JoinPoint joinPoint)
{
System.out.println("=====SysLogAspect后置通知开始=====");
//handleLog(joinPoint, null);
}
/**
* 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "controllerAspect()", throwing = "e")
public void doAfter(JoinPoint joinPoint, Exception e)
{
System.out.println("=====SysLogAspect异常通知开始=====");
//handleLog(joinPoint, e);
}
/**
* 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。
* @param joinPoint
*/
@Around("controllerAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("=====SysLogAspect 环绕通知开始=====");
//handleLog(joinPoint, null);
Object obj= joinPoint.proceed();
System.out.println("=====SysLogAspect 环绕通知结束=====");
return obj;
}
/**
* 日志处理
*
* @param joinPoint
* @param e
*/
private void handleLog(JoinPoint joinPoint, Exception e)
{
try
{
//获得注解
OperationLogger logger = giveController(joinPoint);
if (logger == null)
{
return;
}
String signature = joinPoint.getSignature().toString(); // 获取目标方法签名
String methodName = signature.substring(signature.lastIndexOf(".") + 1,
signature.indexOf("("));
String longTemp = joinPoint.getStaticPart().toLongString();
String classType = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(classType);
Method[] methods = clazz.getDeclaredMethods();
System.out.println("methodName: " + methodName);
for (Method method : methods)
{
if (method.isAnnotationPresent(OperationLogger.class)
&& method.getName().equals(methodName))
{
//OpLogger logger = method.getAnnotation(OpLogger.class);
String clazzName = clazz.getName();
System.out.println("clazzName: " + clazzName + ", methodName: "
+ methodName);
}
}
} catch (Exception exp)
{
logger.error("异常信息:{}", exp);
exp.printStackTrace();
}
}
/**
* 获得注解
* @param joinPoint
* @return
* @throws Exception
*/
private static OperationLogger giveController(JoinPoint joinPoint) throws Exception
{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
return method.getAnnotation(OperationLogger.class);
}
return null;
}
}
控制类
@OperationLogger(modelName = "user", option = "")
@RequestMapping("")
public ModelAndView main(){
ModelAndView mav = new ModelAndView();
System.out.println("哈哈");
mav.setViewName("security/user/user");
return mav;
}
输出结果
=====SysLogAspect 环绕通知开始=====
=====SysLogAspect前置通知开始=====
=====SysLogAspect 环绕通知开始=====
=====SysLogAspect前置通知开始=====
哈哈
=====SysLogAspect 环绕通知结束=====
=====SysLogAspect后置通知开始=====
=====SysLogAspect 环绕通知结束=====
=====SysLogAspect后置通知开始=====