CSDN博客

img jlcool

spring2.0 aop配置

发表于2008/9/28 2:42:00  603人阅读

 
xml文件配置
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:aop="http://www.springframework.org/schema/aop"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
  9.     <bean id="logAdvice" class="cn.com.y234.zteam.tool.aop.LogAdvice" />
  10.     <aop:config>
  11.         <aop:aspect id="logBefore" ref="logAdvice">
  12.             <aop:before
  13.                 pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
  14.                 method="before" />
  15.             <aop:after-returning
  16.                 pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
  17.                 method="afterReturning" />
  18.             <aop:after-throwing
  19.                 pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
  20.                 throwing="throwable" method="afterThrowing" />
  21.             <aop:around
  22.                 pointcut="execution(* cn.com.y234.zteam.biz.IEmployeeBiz.*(..))"
  23.                 method="around" />
  24.         </aop:aspect>
  25.     </aop:config>
  26. </beans>

advice配置

 

  1. package cn.com.y234.zteam.tool.aop;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Arrays;
  5. import java.util.Date;
  6. import org.apache.log4j.Logger;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.ProceedingJoinPoint;
  9. public class LogAdvice {
  10.     private static Logger logger = Logger.getLogger(LogAdvice.class);
  11.     private static DateFormat sdf = new SimpleDateFormat(
  12.             "yyyy年MM月dd日 hh时mm分ss秒");
  13.      //前置通知
  14.     public void before(JoinPoint joinPoint) throws Throwable {
  15.         logger.info("前[系统日志][" + sdf.format(new Date()) + "]"
  16.                 + joinPoint.getTarget() + "("
  17.                 + Arrays.toString(joinPoint.getArgs()) + ")");
  18.     }
  19.      //后置通知
  20.     public void afterReturning(JoinPoint joinPoint) throws Throwable {
  21.         logger.info("后[系统日志][" + sdf.format(new Date()) + "]"
  22.                 + joinPoint.getTarget() + "("
  23.                 + Arrays.toString(joinPoint.getArgs()) + ")");
  24.     }
  25.      //环绕通知
  26.     public void around(ProceedingJoinPoint joinPoint) throws Throwable {
  27.         logger.info("环绕[系统日志][" + sdf.format(new Date()) + "]"
  28.                 + joinPoint.getTarget() + "("
  29.                 + Arrays.toString(joinPoint.getArgs()) + ")");
  30.     }
  31.      //异常通知
  32.     public void afterThrowing(JoinPoint joinPoint, Throwable throwable)
  33.             throws Throwable {
  34.         logger.info("异常[系统日志][" + sdf.format(new Date()) + "]"
  35.                 + joinPoint.getTarget() + "("
  36.                 + Arrays.toString(joinPoint.getArgs()) + ")");
  37.     }
  38. }

 

0 0

相关博文

我的热门文章

img
取 消
img