自定义注解举个代码例子给我看
时间: 2023-08-02 12:43:10 浏览: 48
好的,以下是一个自定义注解的例子:
```
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "";
int count() default 1;
}
```
上面的代码定义了一个名为MyAnnotation的注解,它有两个成员变量value和count,默认值分别为""和1。这个注解可以被用在方法上。
下面是一个使用MyAnnotation注解的例子:
```
public class MyClass {
@MyAnnotation(value = "hello", count = 3)
public void myMethod() {
System.out.println("My method is called.");
}
}
```
上面的代码定义了一个名为MyClass的类,其中的myMethod()方法被使用了MyAnnotation注解,并指定了注解的value和count成员变量的值。
在程序运行时,可以使用Java反射机制来获取类中被注解了MyAnnotation的方法,并获取注解中的元数据信息。
相关问题
Java怎么针对判空写一个自定义注解, 给我举个例子
要针对判空写一个自定义注解,你可以使用 Java 的注解和反射机制来实现。下面是一个示例代码,演示如何创建一个自定义的 `@NotNull` 注解来进行非空校验:
首先,创建一个 `NotNull` 注解:
```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.FIELD)
public @interface NotNull {
}
```
在上述代码中,我们使用 `@Retention(RetentionPolicy.RUNTIME)` 注解指定了注解的保留策略为运行时。而 `@Target(ElementType.FIELD)` 注解表示该注解可以用于字段上。
接下来,创建一个测试类来使用 `@NotNull` 注解:
```java
public class Person {
@NotNull
private String name;
public Person(String name) {
this.name = name;
}
public static void main(String[] args) {
Person person1 = new Person("John");
validateNotNullFields(person1);
Person person2 = new Person(null);
validateNotNullFields(person2);
}
private static void validateNotNullFields(Object object) {
for (Field field : object.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(NotNull.class)) {
field.setAccessible(true);
try {
Object value = field.get(object);
if (value == null) {
throw new IllegalArgumentException("Field '" + field.getName() + "' cannot be null");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
```
在上述代码中,我们创建了一个 `Person` 类,并在 `name` 字段上使用了 `@NotNull` 注解。然后,我们在 `main()` 方法中创建了两个 `Person` 对象,一个给了有效的值,另一个给了 `null`。
接着,我们定义了一个 `validateNotNullFields()` 方法,它使用反射来检查对象中带有 `@NotNull` 注解的字段是否为 `null`。如果字段为 `null`,则抛出一个异常。
在上述示例中,`person1` 没有抛出异常,因为 `name` 字段有一个有效的值。而 `person2` 会抛出异常,因为 `name` 字段的值为 `null`。
这只是一个简单的示例,你可以根据自己的需求进行扩展和定制。希望对你有所帮助!
java中自定义注解如何实现注入,给一个例子
好的,关于java中自定义注解的注入实现,一般是通过反射机制来实现的。具体的实现需要遵循一定的规范,首先要定义一个注解,然后在需要使用的地方进行注解的声明,最后在相应的代码中使用反射机制来获取注解信息并实现注入。
以下是一个简单的示例代码:
```
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String value() default "";
}
public class TestClass {
@TestAnnotation(value = "testValue")
private String testField;
public TestClass() {
// 使用反射获取注解信息
for(Field field : this.getClass().getDeclaredFields()) {
if(field.isAnnotationPresent(TestAnnotation.class)) {
TestAnnotation annotation = field.getAnnotation(TestAnnotation.class);
field.setAccessible(true);
try {
field.set(this, annotation.value());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TestClass test = new TestClass();
System.out.println(test.testField); // 输出 "testValue"
}
}
```
这个例子中定义了一个名为 TestAnnotation 的注解,用来注解 TestClass 中的字段。在 TestClass 中,使用反射遍历所有的字段,如果存在 TestAnnotation 的注解,则将注解中的值赋值给该字段。最后在 main 方法中,创建 TestClass 的实例,输出 testField 字段的值。