spring AOP 拦截日志

本文介绍如何使用Spring AOP实现方法级别的日志拦截。通过配置AOP代理及切面,实现了对特定业务方法的调用进行日志记录。具体包括配置Spring AOP所需依赖、配置文件、定义拦截规则及日志处理类。

1   首先引入jar包 
    spring.jar  aopalliance.jar  aspectjrt.jar  aspectjtools.jar  aspectjweaver.jar  cglib-nodep-2.1_3.jar  cglib-src-2.2.jar    这些个jar包不一定都用得上,但离配置到现在整理已经比较久了  有点忘了.

 

 

2  spring 配置文件头 

<?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"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:jee="http://www.springframework.org/schema/jee"
 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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

 

 

3   定义 aop拦截

 

  <aop:aspectj-autoproxy proxy-target-class="true" />
   <aop:config proxy-target-class="true">
  <aop:aspect ref="logMonitor">
   <aop:pointcut id="actionPointcut" expression="execution(* com.*.*.*.service.impl.*.create*(..)) || execution(* com.*.*.*.service.impl.*.update*(..)) || execution(* com.*.*.*.service.impl.*.delete*(..))|| execution(* com.*.*.*.service.impl.*.insert*(..))||execution(* com.*.*.service.impl.*.create*(..)) || execution(* com.*.*.service.impl.*.update*(..)) || execution(* com.*.*.service.impl.*.delete*(..))|| execution(* com.*.*.service.impl.*.insert*(..))"/>
   <aop:before pointcut-ref="actionPointcut" method="writeLogInfo" />
  </aop:aspect>
   </aop:config>

 

 

  <bean id="logMonitor" parent="baseTransactionProxy">
   <property name="target">
    <bean
     class="com.*.common.userlog.monitor.LogMonitor">
     <property name="userLogInfoService">
      <ref bean="userLogInfoService" />
     </property>
    </bean>
   </property>
 </bean>

 

 

4  处理拦截的类logMonitor

@Aspect
public class LogMonitor {

 /** * 保存变量的ThreadLocal,保持在同一线程中同步数据. */
 private static final ThreadLocal SESSION_MAP = new ThreadLocal();

 private IUserLogInfoService userLogInfoService;

 public IUserLogInfoService getUserLogInfoService() {
  return userLogInfoService;
 }

 public void setUserLogInfoService(IUserLogInfoService userLogInfoService) {
  this.userLogInfoService = userLogInfoService;
 }

 /**
  * 获得线程中保存的属性.
  *
  * @param attribute
  *            属性名称
  * @return 属性值
  */
 public static Object get(String attribute) {
  Map map = (Map) SESSION_MAP.get();
  return map.get(attribute);
 }

 /**
  * 记录日志信息
  * @param joinpoint
  * @throws Exception
  * @throws IllegalAccessException
  */
 public void writeLogInfo(JoinPoint joinpoint) throws Exception, IllegalAccessException {

  UserInfoVO sysUse = (UserInfoVO) UserSession.get(Contents.SESSION_USER_KEY);

 

//  在基类action中 设用户信息

// UserSession.set(Contents.SESSION_USER_KEY, userVO);
// UserSession.set(Contents.SESSION_USER_IP, request.getRemoteAddr());


  String userIP = (String) UserSession.get(Contents.SESSION_USER_IP);

  String temp = joinpoint.getStaticPart().toShortString();
  // 获得操作类
  String classType = joinpoint.getTarget().getClass().getName();
  // 获得方法名称
  String methodName = temp.substring(10, temp.length() - 1);

  Class className = Class.forName(classType);
  // 获得方法的参数类型数组
  Object[] a = joinpoint.getArgs();
  Class[] o = new Class[a.length];//

  for (int i = 0; i < a.length; i++) {
   o[i] = a[i].getClass();
  }
  Method method = className.getMethod(methodName, o);
  LogDesc desc = method.getAnnotation(LogDesc.class);
  // 获得方法的操作动作类型说明
  String actionDesc = "";
  if (desc != null) {
   actionDesc = desc.actionDesc();
  }
  // 记录日志信息
  UserLogInfo userLogInfo = new UserLogInfo();
  if (sysUse != null) {
   userLogInfo.setUserID(sysUse.getUserID());
   userLogInfo.setUserIp(userIP);
   userLogInfo.setOperateClass(classType);
   userLogInfo.setOperateMethod(methodName);
   //设置为一般日志信息标识位
   userLogInfo.setEnableFlag("C");
   userLogInfo.setOperateDescribe(actionDesc);

  }
  userLogInfoService.addUserLog(userLogInfo);

 }

 

 

5   定义取得拦截方法描述的类

   @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

public @interface LogDesc {
    String actionDesc();
}

 

6  在每个需要拦截的方法头加上

 

 /**
  * 注册新用户
  *
  * @roseuid 3C30BD930399
  */
 @LogDesc(actionDesc = "新建会员")
 public MemberVO createMember(MemberVO memberVO) throws SysComException {
  return memberRegisterDao.createMember(memberVO);
 }

 

 

7    设置、取得用户信息

public class UserSession{
// private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();
//
// public  HttpServletRequest getContext(){
// return (HttpServletRequest)threadLocal.get();
// }
// public  void setContext(HttpServletRequest request){
// threadLocal.set(request);
// }
//
// public void cleanContext(){
// threadLocal.set(null);
// }
 /** * 保存变量的ThreadLocal,保持在同一线程中同步数据. */ 
    private static final ThreadLocal SESSION_MAP = new ThreadLocal();  
 
    /** * 工具类的protected构造方法. */ 
    protected UserSession() {  
    }  
 
    /** 
     * 获得线程中保存的属性. 
     *  
     * @param attribute 
     *            属性名称 
     * @return 属性值 
     */ 
    public static Object get(String attribute) {  
        Map map = (Map) SESSION_MAP.get();  
        //System.out.println(map.toString());  
       // System.out.println(map.containsKey("usersession"));  
 
        return map.get(attribute);  
    }  
 
    /** 
     * 获得线程中保存的属性,使用指定类型进行转型. 
     *  
     * @param attribute 
     *            属性名称 
     * @param clazz 
     *            类型 
     * @param <T> 
     *            自动转型 
     * @return 属性值 
     */ 
    public static <T> T get(String attribute, Class<T> clazz) {  
        return (T) get(attribute);  
    }  
 
    /** 
     * 设置制定属性名的值. 
     *  
     * @param attribute 
     *            属性名称 
     * @param value 
     *            属性值 
     */ 
    public static void set(String attribute, Object value) {  
        Map map = (Map) SESSION_MAP.get();  
 
        if (map == null) {  
            map = new HashMap();  
            SESSION_MAP.set(map);  
        }  
 
        map.put(attribute, value);  
    }

 

  

8  另外在配置aop必须是接口的, 否则会出错

   看是否可以在action设置拦截效果应该更好的。

下载方式:https://pan.quark.cn/s/a4b39357ea24 在纺织制造领域中,纱线的品质水平对最终制成品的整体质量具有决定性作用。 鉴于消费者对于产品规格和样式要求的不断变化,纺织制造工艺的执行过程日益呈现为一种更为复杂的操作体系,进而导致对纱线质量进行预测的任务变得更加困难。 在众多预测技术中,传统的预测手段在面对多变量间相互交织的复杂关系时,往往显得力不从心。 因此,智能计算技术在预测纱线质量的应用场景中逐渐占据核心地位,其中人工神经网络凭借其卓越的非线性映射特性以及自适应学习机制,成为了众多预测方法中的一种重要选择。 在智能计算技术的范畴内,粒子群优化算法(PSO)和反向传播神经网络(BP神经网络)是两种被广泛采用的技术方案。 粒子群优化算法是一种基于群体智能理念的优化技术,它通过模拟鸟类的群体觅食行为来寻求最优解,该算法因其操作简便、执行高效以及具备优秀的全局搜索性能,在函数优化、神经网络训练等多个领域得到了普遍应用。 反向传播神经网络则是一种由多层节点构成的前馈神经网络,它通过误差反向传播的机制来实现网络权重和阈值的动态调整,从而达成学习与预测的目标。 在实际操作层面,反向传播神经网络因其架构设计简洁、实现过程便捷,因此被广泛部署于各类预测和分类任务之中。 然而,该方法也存在一些固有的局限性,例如容易陷入局部最优状态、网络收敛过程缓慢等问题。 而粒子群优化算法在参与神经网络优化时,能够显著增强神经网络的全局搜索性能并提升收敛速度,有效规避神经网络陷入局部最优的困境。 将粒子群优化算法与反向传播神经网络相结合形成的PSO-BP神经网络,通过运用粒子群优化算法对反向传播神经网络的权值和阈值进行精细化调整,能够在预测纱线断裂强度方面,显著提升预测结果的...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值