java自定义注解为空值_java自定义注解
1. Java注解(Annotation)
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(value = "unchecked")
壓制編輯器警告
Java元注解
作用:元注解用于修飾其他的注解
@Retention:定義注解的保留策略
@Retention(RetentionPolicy.SOURCE) ????????????//注解僅存在于源碼中,在class字節碼文件中不包含
@Retention(RetentionPolicy.CLASS) ?????????????//默認的保留策略,注解會在class字節碼文件中存在,但運行時無法獲得,
@Retention(RetentionPolicy.RUNTIME) ???????????//注解會在class字節碼文件中存在,在運行時可以通過反射獲取到
@Target:指定被修飾的Annotation可以放置的位置(被修飾的目標)
@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}),也就是此注解可以在方法和類上面使用
@Inherited:指定被修飾的Annotation將具有繼承性
@Documented:指定被修飾的該Annotation可以被javadoc工具提取成文檔.
自定義注解
注解分類(根據Annotation是否包含成員變量,可以把Annotation分為兩類):
標記Annotation:
沒有成員變量的Annotation; 這種Annotation僅利用自身的存在與否來提供信息
元數據Annotation:
包含成員變量的Annotation; 它們可以接受(和提供)更多的元數據;
如何自定義注解?
使用@interface關鍵字,其定義過程與定義接口非常類似,需要注意的是:
Annotation的成員變量在Annotation定義中是以無參的方法形式來聲明的,其方法名和返回值類型定義了該成員變量的名字和類型,
而且我們還可以使用default關鍵字為這個成員變量設定默認值;
注意:只有名字為“value”屬性,賦值時可以省略屬性名
案例一(獲取類與方法上的注解值)
packagecom.ssm.yuan.p1;public enumTranscationModel {
Read, Write, ReadWrite
}
1 packagecom.ssm.yuan.p1;2
3 import java.lang.annotation.*;4
5 /**
6 *7 * MyAnnotation3注解可以用在方法上8 * 注解運行期也保留9 * 可繼承10 */
11 @Target(ElementType.METHOD)12 @Retention(RetentionPolicy.RUNTIME)13 @Inherited14 @Documented15 public @interfaceMyAnnotation3 {16 TranscationModel[] models() defaultTranscationModel.ReadWrite;17 }
1 packagecom.ssm.yuan.p1;2
3 import java.lang.annotation.*;4
5 /**
6 *7 * MyAnnotation2注解可以用在方法上8 * 注解運行期也保留9 * 不可繼承10 */
11 @Target(ElementType.METHOD)12 @Retention(RetentionPolicy.RUNTIME)13 @Documented14 public @interfaceMyAnnotation2 {15 TranscationModel model() defaultTranscationModel.ReadWrite;16 }
1 packagecom.ssm.yuan.p1;2
3 import java.lang.annotation.*;4
5 /**
6 *7 * MyAnnotation1注解可以用在類、接口、屬性、方法上8 * 注解運行期也保留9 * 不可繼承10 */
11 @Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})12 @Retention(RetentionPolicy.RUNTIME)13 @Documented14 public @interfaceMyAnnotation1 {15 String name();16 }
1 packagecom.ssm.yuan.p1;2
3 importorg.junit.Test;4
5 /**
6 *7 */
8 public classDemo1Test {9 @Test10 public void list() throwsException {11 //獲取類上的注解
12 MyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);13 System.out.println(annotation1.name());//abc14
15 //獲取方法上的注解
16 MyAnnotation2 myAnnotation2 = Demo1.class.getMethod("list").getAnnotation(MyAnnotation2.class);17 System.out.println(myAnnotation2.model());//Read
18
19
20
21 }22
23 @Test24 public void edit() throwsException {25 MyAnnotation3 myAnnotation3 = Demo1.class.getMethod("edit").getAnnotation(MyAnnotation3.class);26 for(TranscationModel model : myAnnotation3.models()) {27 System.out.println(model);//Read,Write
28 }29 }30 }
1 packagecom.ssm.yuan.p1;2
3 /**
4 *5 * 獲取類與方法上的注解值6 */
7 @MyAnnotation1(name = "abc")8 public classDemo1 {9
10 @MyAnnotation1(name = "xyz")11 privateInteger age;12
13 @MyAnnotation2(model =TranscationModel.Read)14 public voidlist() {15 System.out.println("list");16 }17
18 @MyAnnotation3(models ={TranscationModel.Read, TranscationModel.Write})19 public voidedit() {20 System.out.println("edit");21 }22 }
案例一測試結果
案例二(獲取類屬性上的注解屬性值)
1 packagecom.ssm.yuan.p2;2
3 importjava.lang.annotation.ElementType;4 importjava.lang.annotation.Retention;5 importjava.lang.annotation.RetentionPolicy;6 importjava.lang.annotation.Target;7
8 /**
9 */
10 //@Retention(RetentionPolicy.SOURCE)
11 @Retention(RetentionPolicy.RUNTIME)12 @Target(ElementType.FIELD)13 public @interfaceTestAnnotation {14 String value() default "默認value值";15
16 String what() default "這里是默認的what屬性對應的值";17 }
1 packagecom.ssm.yuan.p2;2
3 /**
4 *5 * 獲取類屬性上的注解屬性值6 */
7 public classDemo2 {8 @TestAnnotation(value = "這就是value對應的值_msg1", what = "這就是what對應的值_msg1")9 private staticString msg1;10
11 @TestAnnotation("這就是value對應的值1")12 private staticString msg2;13
14 @TestAnnotation(value = "這就是value對應的值2")15 private staticString msg3;16
17 @TestAnnotation(what = "這就是what對應的值")18 private staticString msg4;19 }
1 packagecom.ssm.yuan.p2;2
3 importorg.junit.Test;4
5 /**
6 */
7 public classDemo2Test {8 @Test9 public void test1() throwsException {10 TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class);11 System.out.println(msg1.value());12 System.out.println(msg1.what());13 }14
15 @Test16 public void test2() throwsException{17 TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class);18 System.out.println(msg2.value());19 System.out.println(msg2.what());20 }21
22 @Test23 public void test3() throwsException{24 TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class);25 System.out.println(msg3.value());26 System.out.println(msg3.what());27 }28
29 @Test30 public void test4() throwsException{31 TestAnnotation msg4 = Demo2.class.getDeclaredField("msg4").getAnnotation(TestAnnotation.class);32 System.out.println(msg4.value());33 System.out.println(msg4.what());34 }35 }
案例三(獲取參數修飾注解對應的屬性值)
1 packagecom.ssm.yuan.p3;2
3 import java.lang.annotation.*;4
5 /**
6 *7 * 非空注解:使用在方法的參數上,false表示此參數可以為空,true不能為空8 */
9 @Documented10 @Target({ElementType.PARAMETER})11 @Retention(RetentionPolicy.RUNTIME)12 public @interfaceIsNotNull {13 boolean value() default false;14 }
1 packagecom.ssm.yuan.p3;2
3 /**
4 *5 * 獲取參數修飾注解對應的屬性值6 */
7 public classDemo3 {8
9 public void hello1(@IsNotNull(true) String name, @IsNotNull(false) Integer age) {10 System.out.println("hello:" +name);11 }12
13 public voidhello2(@IsNotNull String name) {14 System.out.println("hello:" +name);15 }16 }
1 packagecom.ssm.yuan.p3;2
3 importorg.junit.Test;4
5 importjava.lang.reflect.Parameter;6
7 /**
8 */
9 public classDemo3Test {10
11 @Test12 public void hello1() throwsException {13 Demo3 demo3 = newDemo3();14 for (Parameter parameter : demo3.getClass().getMethod("hello1", String.class, Integer.class).getParameters()) {15 IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);16 if(annotation != null){17 System.out.println(annotation.value());//true
18 }19 }20 }21
22 @Test23 public void hello2() throwsException {24 Demo3 demo3 = newDemo3();25 for (Parameter parameter : demo3.getClass().getMethod("hello2", String.class).getParameters()) {26 IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);27 if(annotation != null){28 System.out.println(annotation.value());//false
29 }30 }31 }32 }
Aop自定義注解的應用
1 packagecom.ssm.yuan.springaop;2
3 importjava.lang.annotation.ElementType;4 importjava.lang.annotation.Retention;5 importjava.lang.annotation.RetentionPolicy;6 importjava.lang.annotation.Target;7
8 /**
9 */
10 @Target(ElementType.METHOD)11 @Retention(RetentionPolicy.RUNTIME)12 public @interfaceMyLog {13 String desc();14 }
1 packagecom.ssm.yuan.component;2
3 importcom.ssm.yuan.springaop.MyLog;4 importorg.aspectj.lang.JoinPoint;5 importorg.aspectj.lang.annotation.Aspect;6 importorg.aspectj.lang.annotation.Before;7 importorg.aspectj.lang.annotation.Pointcut;8 importorg.aspectj.lang.reflect.MethodSignature;9 importorg.slf4j.Logger;10 importorg.slf4j.LoggerFactory;11 importorg.springframework.stereotype.Component;12
13 /**
14 */
15 @Component16 @Aspect17 public classMyLogAspect {18 private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);19
20 /**
21 * 只要用到了com.yuan.p2.annotation.springAop.MyLog這個注解的,就是目標類22 */
23 @Pointcut("@annotation(com.ssm.yuan.springaop.MyLog)")24 private voidMyValid() {25 }26
27 @Before("MyValid()")28 public voidbefore(JoinPoint joinPoint) {29 MethodSignature signature =(MethodSignature) joinPoint.getSignature();30 logger.debug("[" + signature.getName() + " : start.....]");31
32 MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);33 logger.debug("【目標對象方法被調用時候產生的日志,記錄到日志表中】:"+myLog.desc());34 }35 }
Controller層
1 packagecom.ssm.yuan.controller;2
3 importcom.ssm.yuan.springaop.MyLog;4 importorg.springframework.stereotype.Component;5
6 /**
7 */
8 @Component9 public classLogController {10
11 @MyLog(desc = "這是結合spring aop知識,講解自定義注解應用的一個案例")12 public voidtestLogAspect(){13 System.out.println("隨便寫點東西。。。。");14 }15 }
SpringBaseTest
packagecom.ssm.yuan;importcom.ssm.yuan.model.Surfaceregistration;importcom.ssm.jt.util.PageBean;importorg.junit.Before;importorg.junit.runner.RunWith;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml"})public classSpringBaseTest {
}
Controller層測試
1 packagecom.ssm.yuan.controller;2
3 importcom.ssm.yuan.SpringBaseTest;4 importorg.junit.Test;5 importorg.springframework.beans.factory.annotation.Autowired;6
7 /**
8 */
9 public class LogControllerTest extendsSpringBaseTest {10 @Autowired11 privateLogController logController;12
13 @Test14 public voidtestLogAspect(){15 logController.testLogAspect();16 }17 }
謝謝觀看!!!
總結
以上是生活随笔為你收集整理的java自定义注解为空值_java自定义注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 商品领域ddd_DDD领域驱动实战 -
- 下一篇: flutter字体不跟随系统_flutt