如何写一个自定义注解并使用
时间: 2023-09-07 17:11:50 浏览: 65
自定义注解的写法如下所示:
```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
String value() default "";
int count() default 0;
}
```
上述代码定义了一个名为`CustomAnnotation`的自定义注解。该注解具有两个成员变量:`value`和`count`,分别表示注解的值和计数。`@Retention(RetentionPolicy.RUNTIME)`表示该注解在运行时可见,`@Target(ElementType.METHOD)`表示该注解可以应用于方法上。
使用自定义注解的示例代码如下所示:
```java
public class MyClass {
@CustomAnnotation(value = "Hello", count = 5)
public void myMethod() {
// 方法体
}
}
```
在上述示例中,`@CustomAnnotation(value = "Hello", count = 5)`表示在`myMethod`方法上应用了`CustomAnnotation`注解,并设置了`value`为"Hello",`count`为5。
要获取方法上的自定义注解信息,可以使用反射机制。例如,可以使用以下代码获取`myMethod`方法上的`CustomAnnotation`注解信息:
```java
import java.lang.reflect.Method;
public class Main {
public static void main(String\[\] args) throws NoSuchMethodException {
Method method = MyClass.class.getMethod("myMethod");
CustomAnnotation annotation = method.getAnnotation(CustomAnnotation.class);
String value = annotation.value();
int count = annotation.count();
System.out.println("Value: " + value);
System.out.println("Count: " + count);
}
}
```
上述代码中,通过`getMethod`方法获取`myMethod`方法的`Method`对象,然后使用`getAnnotation`方法获取`CustomAnnotation`注解的实例,最后可以通过注解实例的成员变量获取注解的值。
希望以上信息对您有所帮助。如果还有其他问题,请随时提问。
#### 引用[.reference_title]
- *1* [Springboot中如何自定义注解并使用](https://blog.csdn.net/weixin_41657954/article/details/122387469)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [Java中自定义注解的使用](https://blog.csdn.net/weixin_45565886/article/details/127136969)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)