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:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <bean id="logAdvice" class="cn.com.y234.zteam.tool.aop.LogAdvice" />
- <aop:config>
- <aop:aspect id="logBefore" ref="logAdvice">
- <aop:before
- pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
- method="before" />
- <aop:after-returning
- pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
- method="afterReturning" />
- <aop:after-throwing
- pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
- throwing="throwable" method="afterThrowing" />
- <aop:around
- pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
- method="around" />
- </aop:aspect>
- </aop:config>
- </beans>
advice配置
- package cn.com.y234.zteam.tool.aop;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Date;
- import org.apache.log4j.Logger;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- public class LogAdvice {
- private static Logger logger = Logger.getLogger(LogAdvice.class);
- private static DateFormat sdf = new SimpleDateFormat(
- "yyyy年MM月dd日 hh时mm分ss秒");
- //前置通知
- public void before(JoinPoint joinPoint) throws Throwable {
- logger.info("前[系统日志][" + sdf.format(new Date()) + "]"
- + joinPoint.getTarget() + "("
- + Arrays.toString(joinPoint.getArgs()) + ")");
- }
- //后置通知
- public void afterReturning(JoinPoint joinPoint) throws Throwable {
- logger.info("后[系统日志][" + sdf.format(new Date()) + "]"
- + joinPoint.getTarget() + "("
- + Arrays.toString(joinPoint.getArgs()) + ")");
- }
- //环绕通知
- public void around(ProceedingJoinPoint joinPoint) throws Throwable {
- logger.info("环绕[系统日志][" + sdf.format(new Date()) + "]"
- + joinPoint.getTarget() + "("
- + Arrays.toString(joinPoint.getArgs()) + ")");
- }
- //异常通知
- public void afterThrowing(JoinPoint joinPoint, Throwable throwable)
- throws Throwable {
- logger.info("异常[系统日志][" + sdf.format(new Date()) + "]"
- + joinPoint.getTarget() + "("
- + Arrays.toString(joinPoint.getArgs()) + ")");
- }
- }
本文介绍了一个使用Spring AOP实现的日志记录方案。通过定义一个名为LogAdvice的切面,该方案能够在指定的方法调用前后及出现异常时记录日志,增强了系统的可维护性和可监控性。
2216

被折叠的 条评论
为什么被折叠?



