Aop AfterReturning enhancement method returns a value

Requirements: check your order to return the user name

  In order to decouple, check your order does not query the user, automatically inject user name using aop

    Note: Orders in the list of users to cache memory, traversing inquiry soon, if the Direct Access database, the efficiency is relatively low

 

Idea: to strengthen the return value (aop return value enhancement, injected into the Orders table userName)

  1. Notes

/ ** 
 * non-empty set of attributes switches 
 * Only add this note on the method, will add to the property assignment FieldNotNull Field, 
 * / 
@Documented 
@Retention (RetentionPolicy.RUNTIME) 
@Target (ElementType.METHOD) 
public @ interface SetFieldSwitch { 
}

  

/ ** 
 * Notes field is not empty 
 * / 
@Documented 
@Retention (RetentionPolicy.RUNTIME) 
@Target (ElementType.FIELD)           // role in the field (aop tell which object to which method call, what param parameters need to pass, the query the results which need to take a targetField) 
public @ interface FieldNotNull { 

    class beanClass ();   // needs to call the class to which the properties (from the userName) 

    String method ();     // which method to call the class 

    String param ();      // method call parameters 

    String targetField ();    // which values need to call a method (in order to set the properties of the added annotation) 
    
}

  2: Orders + user object

 

@Data
public class UserOrder /*extends Order*/ {
    private Integer id;

    private Integer goodsId;

    private Integer userId;

    @FieldNotNull(beanClass = UserCache.class, method = "get", param = "userId", targetField = "realName")
    private String userName;    //用户名

    private String goodName;    //物品名称

    public UserOrder(Integer id, Integer userId, Integer goodId, String userName, String goodName) {
        this.userName = userName;
        this.goodName = goodName;
        this.setId(id);
        this.setUserId(userId);
        this.setGoodsId(goodId);
    }

}

 

3: Check Order Method

 

@Service
 public  class OrderService { 
    @Autowired 
    Private OrderDao OrderDao; 

    @SetFieldSwitch // Open aop enhanced (if not enabled, the annotation FileNotNull not work) to separate control and implementation) 
    public List <UserOrder> listOrder () {
         return OrderDao .listOrder (); 
    } 
}

 

 

4: queries the user's method

 

5: AOP section

    Use AfterReturning

 

    /**
     *
     * @param point 切点
     * @param obj   返回值
     * @return
     * @throws Throwable
     */
    @AfterReturning(value = "setFieldValuePoint()", returning = "obj")
    public Object setValue(JoinPoint point, Object obj) throws Throwable {

        beanUtils.setFieldValueForCollection((Collection) obj);
        return obj;
    }

 

 

 

 

Late at night, to be continued .....

 

 

 

 

   

 

 

  

Guess you like

Origin www.cnblogs.com/draymond/p/12670280.html