zoukankan      html  css  js  c++  java
  • 实训笔记

    1.@Aspect 

     1 package com.tianque.project.recruit.aspect;
     2 
     3 import com.tianque.project.core.thread.ThreadVariable;
     4 import com.tianque.project.recruit.domain.RecruitUserDetail;
     5 import org.aspectj.lang.annotation.Aspect;
     6 import org.aspectj.lang.annotation.Before;
     7 import org.slf4j.Logger;
     8 import org.slf4j.LoggerFactory;
     9 import org.springframework.stereotype.Repository;
    10 
    11 import java.lang.reflect.InvocationTargetException;
    12 import java.lang.reflect.Method;
    13 import java.util.Date;
    14 
    15 
    16 @Aspect
    17 @Repository("recruitAspect")
    18 public class RecruitAspect {
    19     private final static Logger logger = LoggerFactory.getLogger(RecruitAspect.class);
    20 
    21     //新增
    22     @Before("execution(public * com.tianque..*.Recruit*Mapper.add*(..))&&  args(obj,..)")
    23     public void appendCreateInfo(Object obj) {
    24         Method setCreateUserId = null;
    25         Method setCreateDate = null;
    26         try {
    27             setCreateUserId = obj.getClass().getMethod("setCreateUserId", Long.class);
    28         } catch (NoSuchMethodException e) {
    29             //DO NOTHING
    30         }
    31         try {
    32             setCreateDate = obj.getClass().getMethod("setCreateDate", Date.class);
    33         } catch (NoSuchMethodException e) {
    34             //DO NOTHING
    35         }
    36         Long crrentUserId = ThreadVariable.getSession().getUserId();
    37         if (null != setCreateUserId && null != crrentUserId) {
    38             try {
    39                 setCreateUserId.invoke(obj, ThreadVariable.getSession().getUserId());
    40             } catch (IllegalAccessException | InvocationTargetException e) {
    41                 logger.error("设置创建人出现异常", e);
    42             }
    43         }
    44         if (null != setCreateDate) {
    45             try {
    46                 setCreateDate.invoke(obj, new Date());
    47             } catch (IllegalAccessException | InvocationTargetException e) {
    48                 logger.error("设置创建人时间出现异常", e);
    49             }
    50         }
    51         //如果是完善個人資料,將id设为当前用户ID
    52         if (obj instanceof RecruitUserDetail) {
    53             ((RecruitUserDetail) obj).setId(crrentUserId);
    54         }
    55     }
    56 
    57     //更新
    58     @Before("execution(public * com.tianque..*.Recruit*Mapper.aupdate*(..))&&  args(obj,..)")
    59     public void appendUpdateInfo(Object obj) {
    60         Method setUpdateUserId = null;
    61         Method setUpdateDate = null;
    62         try {
    63             setUpdateUserId = obj.getClass().getMethod("setUpdateUserId", Long.class);
    64         } catch (NoSuchMethodException e) {
    65             //DO NOTHING
    66         }
    67         try {
    68             setUpdateDate = obj.getClass().getMethod("setUpdateDate", Date.class);
    69         } catch (NoSuchMethodException e) {
    70             //DO NOTHING
    71         }
    72 
    73         if (null != setUpdateUserId) {
    74             try {
    75                 setUpdateUserId.invoke(obj, ThreadVariable.getSession().getUserId());
    76             } catch (IllegalAccessException | InvocationTargetException e) {
    77                 logger.error("设置修改人出现异常", e);
    78             }
    79         }
    80 
    81         if (null != setUpdateDate) {
    82             try {
    83                 setUpdateDate.invoke(obj, new Date());
    84             } catch (IllegalAccessException | InvocationTargetException e) {
    85                 logger.error("设置修改时间出现异常", e);
    86             }
    87         }
    88     }
    89 }
    View Code

    2.mybatis返回ID,实际上并不是指返回的结果是id.结果还是处理成功的条数,id会被set到对象参数的属性ID里。

    3.memcache.

    以前面试背的,memcache只能保存String,然后出去面试胡吹牛,实际上,只要保存的对象不管是类还是集合,只要实现了序列化接口都是可以保存的。

    4.事务的传播行为:

    Spring中事务的定义:
    一、Propagation :
      key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:
    PROPAGATION_REQUIRED–支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
    PROPAGATION_SUPPORTS–支持当前事务,如果当前没有事务,就以非事务方式执行。 
    PROPAGATION_MANDATORY–支持当前事务,如果当前没有事务,就抛出异常。 
    PROPAGATION_REQUIRES_NEW–新建事务,如果当前存在事务,把当前事务挂起。 
    PROPAGATION_NOT_SUPPORTED–以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
    PROPAGATION_NEVER–以非事务方式执行,如果当前存在事务,则抛出异常。 

    很多人看到事务的传播行为属性都不甚了解,我昨晚看了j2ee without ejb的时候,看到这里也不了解,甚至重新翻起数据库系统的教材书,但是也没有找到对这个的分析。今天搜索,找到一篇极好的分析文章,虽然这篇文章是重点分析PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRED_NESTED的
    解惑 spring 嵌套事务
    ********TransactionDefinition 接口定义*******************

         int PROPAGATION_REQUIRED = 0;   

         int PROPAGATION_SUPPORTS = 1;   
         int PROPAGATION_MANDATORY = 2;   

         int PROPAGATION_REQUIRES_NEW = 3;   

         int PROPAGATION_NOT_SUPPORTED = 4;   
         int PROPAGATION_NEVER = 5;   

         int PROPAGATION_NESTED = 6;   

    *************************************************************************
    在这篇文章里,他用两个嵌套的例子辅助分析,我这里直接引用了。********************sample***********************
    ServiceA {   
           
         void methodA() {   
             ServiceB.methodB();   
         }   
      
    }   
      
    ServiceB {   
           
         
         void methodB() {   
         }   
           
    }      
    *************************************************
    我们这里一个个分析吧
    1: PROPAGATION_REQUIRED 
    加入当前正要执行的事务不在另外一个事务里,那么就起一个新的事务
    比如说,ServiceB.methodB的事务级别定义为PROPAGATION_REQUIRED, 那么由于执行ServiceA.methodA的时候,
             ServiceA.methodA已经起了事务,这时调用ServiceB.methodB,ServiceB.methodB看到自己已经运行在ServiceA.methodA
    的事务内部,就不再起新的事务。而假如ServiceA.methodA运行的时候发现自己没有在事务中,他就会为自己分配一个事务。
    这样,在ServiceA.methodA或者在ServiceB.methodB内的任何地方出现异常,事务都会被回滚。即使ServiceB.methodB的事务已经被
    提交,但是ServiceA.methodA在接下来fail要回滚,ServiceB.methodB也要回滚
    2:   PROPAGATION_SUPPORTS
    如果当前在事务中,即以事务的形式运行,如果当前不再一个事务中,那么就以非事务的形式运行
    这就跟平常用的普通非事务的代码只有一点点区别了。不理这个,因为我也没有觉得有什么区别
    3:   PROPAGATION_MANDATORY
    必须在一个事务中运行。也就是说,他只能被一个父事务调用。否则,他就要抛出异常。
    4:   PROPAGATION_REQUIRES_NEW
    这个就比较绕口了。 比如我们设计ServiceA.methodA的事务级别为PROPAGATION_REQUIRED,ServiceB.methodB的事务级别为PROPAGATION_REQUIRES_NEW,
    那么当执行到ServiceB.methodB的时候,ServiceA.methodA所在的事务就会挂起,ServiceB.methodB会起一个新的事务,等待ServiceB.methodB的事务完成以后,
    他才继续执行。他与PROPAGATION_REQUIRED 的事务区别在于事务的回滚程度了。因为ServiceB.methodB是新起一个事务,那么就是存在
    两个不同的事务。如果ServiceB.methodB已经提交,那么ServiceA.methodA失败回滚,ServiceB.methodB是不会回滚的。如果ServiceB.methodB失败回滚,
    如果他抛出的异常被ServiceA.methodA捕获,ServiceA.methodA事务仍然可能提交。
    5:   PROPAGATION_NOT_SUPPORTED 
    当前不支持事务。比如ServiceA.methodA的事务级别是PROPAGATION_REQUIRED ,而ServiceB.methodB的事务级别是PROPAGATION_NOT_SUPPORTED ,
    那么当执行到ServiceB.methodB时,ServiceA.methodA的事务挂起,而他以非事务的状态运行完,再继续ServiceA.methodA的事务。
    6:   PROPAGATION_NEVER 
    不能在事务中运行。假设ServiceA.methodA的事务级别是PROPAGATION_REQUIRED, 而ServiceB.methodB的事务级别是PROPAGATION_NEVER ,
    那么ServiceB.methodB就要抛出异常了。
    7:   PROPAGATION_NESTED 
    理解Nested的关键是savepoint。他与PROPAGATION_REQUIRES_NEW的区别是,PROPAGATION_REQUIRES_NEW另起一个事务,将会与他的父事务相互独立,
    而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,如果父事务最后回滚,他也要回滚的。
    而Nested事务的好处是他有一个savepoint。
    *****************************************
    ServiceA {   
           
           
         void methodA() {   
             try {
          //savepoint   
                 ServiceB.methodB();    //PROPAGATION_NESTED 级别
             } catch (SomeException) {   
                 // 执行其他业务, 如 ServiceC.methodC();   
             }   
         }   
      
    }   
    ********************************************
    也就是说ServiceB.methodB失败回滚,那么ServiceA.methodA也会回滚到savepoint点上,ServiceA.methodA可以选择另外一个分支,比如
    ServiceC.methodC,继续执行,来尝试完成自己的事务。
    但是这个事务并没有在EJB标准中定义。
  • 相关阅读:
    Simple ASP.NET CORE 2.2 App +Vue JS
    Upload Image to .NET Core 2.1 API
    Nginx支持WebSocket服务
    DD打卡
    Asp.net Core 源码-PagedList<T>
    Asp.net Core 源码-UrlExtensions
    Asp.net Core 源码-SessionExtensions
    树莓派资源集合
    frp内网穿透
    Nuget包含cssjs等资源文件
  • 原文地址:https://www.cnblogs.com/jyzyz/p/10969657.html
Copyright © 2011-2022 走看看