2015年4月4日 星期六

aspect @annotation @around

 package com.lfo.suntone;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.stereotype.Component;

@Component("recordStockHistory")
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public  @interface RecordStockHistory {

}





package com.lfo.suntone.aspect;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lfo.suntone.NotValidException;
import com.lfo.suntone.RecordStockHistory;
import com.lfo.suntone.controller.StockController;
import com.lfo.suntone.dao.DaoUtils;
import com.lfo.suntone.db.Stockhistory;
import com.lfo.suntone.tool.AspectTool;
import com.lfo.vo.MoveRequest;
import com.lfo.vo.Status;
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Aspect
@Component
public class ControllerAspect {

   
    @Autowired
    private  AspectTool aspectTool;
    @Pointcut("@annotation(com.lfo.suntone.RecordStockHistory)")
    public void movePointcut() {
       
    }
   
    @Around("movePointcut()")
    public Object move(ProceedingJoinPoint  j) throws Throwable{
        System.out.println("movePointcut");
        Object[] args = j.getArgs();
         System.out.println("argslen"+args.length);
         MoveRequest m=(MoveRequest)args[0];
         System.out.println("move() m:"+m.getAmount());
        return j.proceed();
       
    }
    /*
    @Pointcut("execution(* com.lfo.suntone.controller.StockController.*(..))")
    public void header() {
       
    }
    @Around("header()")
    public Object   header(ProceedingJoinPoint  j) throws NotValidException {
       
       
        String userid=aspectTool.getHeaderInStockController(j);
        Boolean isvalidid=aspectTool.isValidUserid(userid);
         if(isvalidid){
             try {
               
                 System.out.println("header isValidUserid:");
               
                Object ss = j.proceed();//不但攔截並且取回結果
                 aspectTool.saveStockhistory(j, userid);
               
                return ss  ;//必須回傳否則無法原方法return 為null
            } catch (Throwable e) {
                e.printStackTrace();
            }
         }else{
             System.out.println("header isnot  ValidUserid");
           
             StockController s=(StockController) j.getTarget();
             try {
                s.getResponse().getOutputStream().print("error userid");
            } catch (IOException e) {
               
                e.printStackTrace();
            }
             return null;
           
           
           
         }
        return null;
    }
    */
   
}

沒有留言:

張貼留言