Javaおよびカスタム注釈のAOPを使用

目的:

  1.Javaのコメントプロフィール

  2. Javaのメタアノテーション(フォーカス)

  3.カスタム注釈

      ケースI(クラスやメソッドの値にコメントをもらいます)

      ケースII(取得クラス属性の注釈属性値)

      ケースIII(取得パラメータは注釈対応する属性値を変更されました)

       4. AOPのカスタム注釈アプリケーション(フォーカス)

 


 

アノテーションの概要

1. Javaの注釈(アノテーション)

   Java のアノテーションは、コンパイル中のツールのいくつかのためにいくつかのメタ情報のコードに追加されます

   解析し、使用実行時、プレイ命令、構成機能。

   クラス関連の注意事項は、中に含まれている java.lang.annotationのパッケージ。

 

2. Javaのアノテーションのカテゴリー

  2.1 JDK の基本的な注釈

  2.2 JDKの元ノート

  2.3 カスタム注釈

 

3. JDK の基本的な注釈

  3.1 @Override

      リライト

 

  3.2 @Deprecated

      廃れました

 

  3.3 @SuppressWarnings(値= "未チェック")

 

      押すとエディタの警告

 


 

Java メタアノテーション

  役割:他のノートの修正のためのメタアノテーション

  • @Retention :注釈定義された保持ポリシー

 

    @Retention(RetentionPolicy.SOURCE)//注釈のみソースコードに存在する、含まれていないクラスのバイトコードファイル
      @Retention(RetentionPolicy.CLASS)//デフォルトの保持ポリシーは、注釈がクラスのバイトコードファイルに存在し、しかし得ることができず、実行中の
      @Retention(RetentionPolicy.RUNTIME)//注釈は、実行時にクラスのバイトコードファイルに存在しますが反射することによって得ることができます

 

  • @target :指定は、修正注釈の位置に配置することができる変性ターゲット

 

      @Target(ElementType.TYPE)                       // インタフェースクラス 
      @Target(ElementType.FIELD)                      // プロパティ 
      @Target(ElementType.METHOD)                     // 方法 
      @Target(ElementType.PARAMETER)                  // メソッドパラメータ 
      @Target(ElementType.CONSTRUCTOR)                // コンストラクタ 
      @Target(ElementType.LOCAL_VARIABLE)             // ローカル変数 
      @Target(ElementType.ANNOTATION_TYPE)            // 注釈 
      @Target(ElementType.PACKAGE)                    // パッケージ 

 注:たとえば、複数の位置を指定することができます。

@Target({ElementType.METHOD、ElementType.TYPE}) すなわち、この注釈は、上記の方法とクラスで使用することができます

 

  • @継承:指定変更注釈を継承しています

 

@Documented:指定は修正注釈は、文書にjavadocツールを抽出することができます。

 


カスタム注釈

ノート分類(に従って注釈がメンバ変数が含まれていることができ、注釈の2つのカテゴリに分類)

マーク注釈:

いいえメンバ変数の注釈ません。この注釈情報を提供するためにのみ、彼らの有無を使用して

メタデータ注釈:

 あってメンバ変数注釈を、彼らは受け入れることができますおよび提供追加のメタデータ

注釈をカスタマイズする方法?

使用 @interfaceのキーワード定義されたプロセスとのインタフェースを定義するには、非常に似ているということに注意すること

   注釈のメンバ変数の注釈は、定義を宣言するノンパラメトリック法の形でそのメソッド名、戻り値型がメンバ変数の名前とタイプを定義し

   そして、我々はまた、使用することができ、デフォルトのメンバ変数のデフォルト値を設定したキーワードを、

 

注:名前だけが」プロパティには、割り当てのプロパティ名を省略することができます

 

ケースI(クラスやメソッド値に対してgetノート):

  TranscationModel

パッケージcom.huangting.annotation.P1を。
/ ** 
 *列挙枚举
 * / 
パブリック 列挙  TranscationModel { 
    読む、読み書き、書き込み
}

  MyAnnotation1

package com.huangting.annotation.P1;

import java.lang.annotation.*;

/**
 * MyAnnotation1注解可以用在类、接口、属性、方法上
 * 注解运行期也保留
 * 不可继承
 */
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
    String name();
}

  MyAnnotation2

package com.huangting.annotation.P1;

import java.lang.annotation.*;

/**
 *  MyAnnotation2注解可以用在方法上
 *  注解运行期也保留
 *  不可继承
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
    TranscationModel model() default TranscationModel.ReadWrite;
}

   MyAnnotation3

package com.huangting.annotation.P1;

import java.lang.annotation.*;

/**
 * MyAnnotation3注解可以用在方法上
 * 注解运行期也保留
 * 可继承
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {
    TranscationModel[] models() default TranscationModel.ReadWrite;
}

 Dome1

package com.huangting.annotation.P1;

/**
 * 获取类与方法上的注解值
 */
@MyAnnotation1(name = "abc")
public class Demo1 {

    @MyAnnotation1(name = "xyz")
    private Integer age;

    @MyAnnotation2(model = TranscationModel.Read)
    public void list() {
        System.out.println("list");
    }

    @MyAnnotation3(models = {TranscationModel.Read, TranscationModel.Write})
    public void edit() {
        System.out.println("edit");
    }
}

Demo1Test

package com.huangting.annotation.P1;
import org.junit.Test;
public class Demo1Test {
    @Test
    public void list() throws Exception {
//        获取类上的注解
        MyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);
        System.out.println(annotation1.name());//abc

//        获取方法上的注解
        MyAnnotation2 myAnnotation2 = Demo1.class.getMethod("list").getAnnotation(MyAnnotation2.class);
        System.out.println(myAnnotation2.model());//Read
    }

    @Test
    public void edit() throws Exception {
        MyAnnotation3 myAnnotation3 = Demo1.class.getMethod("edit").getAnnotation(MyAnnotation3.class);
        for (TranscationModel model : myAnnotation3.models()) {
            System.out.println(model);//Read,Write
        }
    }
}

效果:

 

 

 注意:

jar包的运用中最好不要出现test,把《scope》《/scope》删除即可

否则会报错@Test无法使用

 

案例二(获取类属性上的注解属性值)

TestAnnotation

package com.huangting.annotation.P2;

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

//@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {
    String value() default "默认value值";
    String what() default "这里是默认的what属性对应的值";
}

Dome2

package com.huangting.annotation.P2;

/**
 * 获取类属性上的注解属性值
 */
public class Demo2 {
    @TestAnnotation(value = "这就是value对应的值_msg1", what = "这就是what对应的值_msg1")
    private static String msg1;

    @TestAnnotation("这就是value对应的值1")
    private static String msg2;

    @TestAnnotation(value = "这就是value对应的值2")
    private static String msg3;

    @TestAnnotation(what = "这就是what对应的值")
    private static String msg4;
}
Demo2Test 
package com.huangting.annotation.P2;

import org.junit.Test;

public class Demo2Test {
    @Test
    public void test1() throws Exception {
        TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class);
        System.out.println(msg1.value());
        System.out.println(msg1.what());
    }

    @Test
    public void test2() throws Exception{
        TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class);
        System.out.println(msg2.value());
        System.out.println(msg2.what());
    }

    @Test
    public void test3() throws Exception{
        TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class);
        System.out.println(msg3.value());
        System.out.println(msg3.what());
    }

    @Test
    public void test4() throws Exception{
        TestAnnotation msg4 = Demo2.class.getDeclaredField("msg4").getAnnotation(TestAnnotation.class);
        System.out.println(msg4.value());
        System.out.println(msg4.what());
    }
}

效果:

 

案例三(获取参数修饰注解对应的属性值):

IsNotNull 
package com.huangting.annotation.P3;

import java.lang.annotation.*;

/**
 * 非空注解:使用在方法的参数上,false表示此参数可以为空,true不能为空
 */
@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotNull {
    boolean value() default false;
}
Demo3 
package com.huangting.annotation.P3;

/**
 * 获取参数修饰注解对应的属性值
 */
public class Demo3 {

    public void hello1(@IsNotNull(true) String name) {
        System.out.println("hello:" + name);
    }

    public void hello2(@IsNotNull String name) {
        System.out.println("hello:" + name);
    }
}
Demo3Test 
package com.huangting.annotation.P3;
import org.junit.Test;
import java.lang.reflect.Parameter;
public class Demo3Test {

    @Test
    public void hello1() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello1", String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if(annotation != null){
                System.out.println(annotation.value());//true
            }
        }
    }

    @Test
    public void hello2() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello2", String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if(annotation != null){
                System.out.println(annotation.value());//false
            }
        }
    }
}

效果:

 

 

 


Aop自定义注解的应用

  MyLog 

package com.huangting.annotation.Aop;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
    String desc();
}

 

MyLogAspect 
package com.huangting.annotation.Aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);

    /**
     * 只要用到了com.huangting.annotation.Aop.MyLog这个注解的,就是目标类
     */
    @Pointcut("@annotation(com.huangting.annotation.Aop.MyLog)")
    private void MyValid() {
    }

    @Before("MyValid()")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);
        System.out.println("[" + signature.getName() + " : start.....]");
        System.out.println("【目标对象方法被调用时候产生的日志,记录到日志表中】:"+myLog.desc());
    }
}
BaseTestCase 
package com.huangting.annotation.Aop;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseTestCase {

}
LogController 
package com.huangting.annotation.Aop;

import org.springframework.stereotype.Component;

@Component
public class LogController {

    @MyLog(desc = "这是结合spring aop知识,讲解自定义注解应用的一个案例")
    public void testLogAspect(){
        System.out.println("墙头马上遥相顾");
    }
}
LogControllerTest 
package com.huangting.annotation.Aop;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class LogControllerTest extends BaseTestCase {
    @Autowired
    private LogController logController;

    @Test
    public void testLogAspect(){
        logController.testLogAspect();
    }
}

效果:

 

 

谢谢观看!

 

おすすめ

転載: www.cnblogs.com/huangting/p/11808634.html