學習java註解,初試啟動springboot專案
阿新 • • 發佈:2022-03-24
學習java註解,初試啟動springboot專案
元註解:
元註解的作用就是負責註解其他註解。Java5.0定義了4個標準的meta-annotation型別,它們被用來提供對其它 annotation型別作說明。Java5.0定義的元註解:
1.@Target
2.@Retention
3.@Documented
4.@Inherited
@Target
取值有:用於描述註解的使用範圍
1.CONSTRUCTOR:用於描述構造器
2.FIELD:用於描述域
3.LOCAL_VARIABLE:用於描述區域性變數
4.METHOD:用於描述方法
5.PACKAGE:用於描述包
6.PARAMETER:用於描述引數
7.TYPE:用於描述類、介面(包括註解型別) 或enum宣告
@Retention 表示需要在什麼級別儲存該註釋資訊,用於描述註解的生命週期
取值有:
1.SOURCE:在原始檔中有效(即原始檔保留)
2.CLASS:在class檔案中有效(即class保留)
3.RUNTIME:在執行時有效(即執行時保留)
@Documented 是一個標記註解,沒有成員。
@Retention
表示需要在什麼級別儲存該註釋資訊,用於描述註解的生命週期
利用springboot 啟動寫一個例子
package com.hcmony; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import java.lang.annotation.*; /** * @FileName package PACKAGE_NAME.com.hcmony.MyAnnotation.java * @Creator hcmony * @Created 2017-11-15 16:50:00 */ @Target(ElementType.TYPE) @Documented @Retention(RetentionPolicy.RUNTIME) @SpringBootApplication@ComponentScan
public @interface MyAnnotation {}
package com.hcmony.adwarn; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; @MyAnnotation public class AdWarnApplication { /** * 方法描述:啟動程式 * * @author hcmny 2017年4月22日 下午4:07:50 * @param args */ public static void main(String[] args) { SpringApplication.run(AdWarnApplication.class, args); } }
上面的例子,通過自定義註解 MyAnnotation 就可以直接啟動一個springboot 專案
https://blog.csdn.net/hcmony/article/details/78543092