JAVA自定義註解SpringAOP

 前言:Annotation(註解)是JDK5.0及之後版本引入的,它的做用就是負責註解其餘註解。如今開發過程當中你們都已經放棄了傳統的XML配置的方式改成註解的方式,既簡單又簡潔,方便管理和維護。目前引用第三方jar包的註解都是解決技術上的問題,然而咱們在工做中也須要經過註解解決業務上的一些問題,因此就得用到自定義註解。java

1.自定義一個註解

建立一個 @interface的文件,則表示這是一個註解類web

/*
 * 自定義註解
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SecureValid {
    String desc() default "身份和安全驗證開始...";
}

2.註解的屬性描述

2.1 @Target 

用於描述註解的使用範圍(即:被描述的註解能夠用在什麼地方),其取值有:spring

ElementType.CONSTRUCTOR                用於描述構造器。
ElementType.FIELD                                用於描述域。
ElementType.LOCAL_VARIABLE             用於描述局部變量
ElementType.METHOD                          用於描述方法
ElementType.PACKAGE                         用於描述包
ElementType.PARAMETER                     用於描述參數
ElementType.TYPE                                 用於描述類或接口數組

2.2 @Retention

用於描述註解的生命週期(即:被描述的註解在什麼範圍內有效),其取值有:
RetentionPolicy.SOURCE                        在源文件中有效(即源文件保留)。
RetentionPolicy.CLASS                           在 class 文件中有效(即 class 保留)
RetentionPolicy.RUNTIME                      在運行時有效(即運行時保留)安全

2.3 @Documented

在默認的狀況下javadoc命令不會將咱們的annotation生成再doc中去的,因此使用該標記就是告訴jdk讓它也將annotation生成到doc中去spa

2.4 @Inherited

 好比有一個類A,在他上面有一個標記annotation,那麼A的子類B是否不用再次標記annotation就能夠繼承獲得.net

3.Annotation屬性值

有如下三種: 基本類型、數組類型、枚舉類型code

3.1 基本串類型 

public @interface UserdefinedAnnotation {  
    intvalue();  
    String name() default "zhangsan";  
    String address();  
}
使用:
@UserdefinedAnnotation(value=123,name="wangwenjun",address="火星")  
    public static void main(String[] args) {  
        System.out.println("hello");  
    }  
}

3.2 數組

public @interface UserdefinedAnnotation {  
    int[] value();  
}  
使用:  
public class UseAnnotation {  
      
    @UserdefinedAnnotation({123})  
    public static void main(String[] args) {  
        System.out.println("hello");  
    }  
}

3.3 枚舉類型

public enum DateEnum {  
    Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday  
}  
而後在定義一個annotation  
package com.wangwenjun.annatation.userdefined;  
  
public @interface UserdefinedAnnotation {  
    DateEnum week();  
}  
使用: 
public class UseAnnotation {  
    @UserdefinedAnnotation(week=DateEnum.Sunday)  
    public static void main(String[] args) {  
        System.out.println("hello");  
    }  
}

4.使用SpringAOP來加強

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
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.util.Date;


@Aspect
@Component
public class Aspect {

    private static final Logger logger = LoggerFactory.getLogger(Aspect.class);

    //切入點
    @Pointcut("@annotation(註解的包路徑)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
            //獲取request
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
	        //攔截的實體類
	        Object target = joinPoint.getTarget();
	        //攔截的方法名稱
	        String methodName = joinPoint.getSignature().getName();
	        //攔截的方法參數
	        Object[] args = joinPoint.getArgs();
	        //攔截的放參數類型
	        Class[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();

            
             //TODO 處理業務代碼

             

            //處理完以後放行
            Object[] args = joinPoint.getArgs();
            return joinPoint.proceed(args);
    }
      
}

 

博客地址:https://my.oschina.net/wangnian繼承

相關文章
相關標籤/搜索