1. 程式人生 > >java自定義註釋

java自定義註釋

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

/**
 * @Inherited允許子類繼承父類中的註解
 * @Documented註解表明製作javadoc時,
 * 是否將註解資訊加入文件。如果註解在宣告時使用了@Documented,
 * 則在製作javadoc時註解資訊會加入javadoc。
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
}


@Target (目標,靶子...)

public enum ElementType {
    /** 類、介面(包括註釋型別)或列舉宣告 */
    TYPE,

    /** 欄位宣告(包括列舉常量) */
    FIELD,

    /** 方法宣告 */
    METHOD,

    /** 正式的引數宣告 */
    PARAMETER,

    /** 建構函式宣告 */
    CONSTRUCTOR,

    /** 區域性變數宣告 */
    LOCAL_VARIABLE,

    /** 註釋型別宣告 */
    ANNOTATION_TYPE,

    /** 程式包宣告*/
    PACKAGE,

    /**
     * 型別引數宣告
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * 使用型別
     *
     * @since 1.8
     */
    TYPE_USE
}
@Retention

表示需要再什麼級別儲存該註解資訊,可選的RetentionPolicy引數包括:

SOURCE:註解將被編譯器丟棄。

CLASS:註解再class檔案中可用,但會被VM丟棄。

RUNTIME:VM將在執行期也保留註解,因此可以通過反射機制讀取註解的資訊。

利用AOP對方法級別的註解做相應的操作

package com.fun.utils;

import com.fun.client.domain.MemUserInfo;
import com.fun.client.domain.OperationLog;
import com.fun.client.service.MemUserInfoService;
import com.fun.client.service.OperationLogService;
import com.fun.framework.support.BusinessException;
import com.fun.framework.utils.RequestUtils;
import org.apache.commons.lang3.math.NumberUtils;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class LogAspect {
   /* @Autowired
    private SysLogDao sysLogDao;*/
   @Autowired
   private MemUserInfoService memUserInfoService;
   @Autowired
   private OperationLogService operationLogService;

    @Pointcut("@annotation(com.fun.utils.Log)")
    public void pointcut() { }
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Exception {
        Object result = null;
        long beginTime = System.currentTimeMillis();
        try {
            // 執行方法
            result = point.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        // 執行時長(毫秒)
      //  long time = System.currentTimeMillis() - beginTime;
        // 儲存日誌
        saveLog(point);
        return result;
    }
    private void saveLog(ProceedingJoinPoint joinPoint) throws Exception {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        OperationLog operationLog  = new OperationLog ();

        Log logAnnotation = method.getAnnotation(Log.class);
        if (logAnnotation != null) {
            // 註解上的描述
            operationLog.setContent(logAnnotation.value());
        }
//獲取當前使用者ID(Security框架)
        int userId = NumberUtils.toInt(SecurityContextHolder.getContext().getAuthentication().getName());
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = null ;
        //String ipAddress;
        try {
            if (userId >= 1) {
        //獲取當前使用者登入真實IP(RequestUtils獲取當前登陸者ip等資訊工具類下章有寫)
                 ip = RequestUtils.getIp(request);
                    //解析IP為地址
               //  ipAddress = RequestUtils.getIpAddress(ip);
                   //登入瀏覽器類別
               // String agent = RequestUtils.getAgent(request);
                //  this.memService.addLoginLog(userId, ip, ipAddress, agent);
            }
        } catch (Exception ex2) {
        }
        //插入資料到資料庫
        MemUserInfo memUserInfo = memUserInfoService.selectById(userId);
        operationLog.setMemberId(userId);
        operationLog.setMemberName(memUserInfo.getLoginName());
        operationLog.setOperationIp(ip);
        operationLog.setMemberRemark(memUserInfo.getRemark());
        operationLogService.insert(userId,operationLog);
    }
}