spring 自定义注解

在Java中创建自定义注解

创建自定义注解与编写接口很相似,除了它的接口关键字前有个@符号。我们可以在注解中定义方法,示例如下:

package com.xxx.controller;

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

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {
String value();// 只有一个方法时,默认的方法名为value
}

---------------------------------------------------

多个方法时示例如下;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {
String value();

int age() default 18;//注解方法可以包含默认值

STYLE style() default STYLE.KEEP_ONE;

enum STYLE {
NONE, KEEP_ONE, KEEP_TWO
}
}

1).注解方法不能有参数

2).注解方法的返回类型局限于原始类型,字符串,枚举,注解,或以上类型构成的数组。

3).注解方法可以包含默认值。

4).注解可以包含与其绑定的元注解,元注解为注解提供信息,有四种元注解类型

1. @Documented – 表示使用该注解的元素应被javadoc或类似工具文档化,它应用于类型声明,类型声明的注解会影响客户端对注解元素的使用。如果一个类型声明添加了Documented注解,那么它的注解会成为被注解元素的公共API的一部分。

2. @Target – 表示支持注解的程序元素的种类,一些可能的值有TYPE, METHOD, CONSTRUCTOR, FIELD等等。如果Target元注解不存在,那么该注解就可以使用在任何程序元素之上。

3. @Inherited – 表示一个注解类型会被自动继承,如果用户在类声明的时候查询注解类型,同时类声明中也没有这个类型的注解,那么注解类型会自动查询该类的父类,这个过程将会不停地重复,直到该类型的注解被找到为止,或是到达类结构的顶层(Object)。

4. @Retention – 表示注解类型保留时间的长短,它接收RetentionPolicy参数,可能的值有SOURCE, CLASS, 以及RUNTIME。

Java内置注解

Java提供3种内置注解。

1. @Override – 当我们想要覆盖父类的一个方法时,需要使用该注解告知编译器我们正在覆盖一个方法。这样的话,当父类的方法被删除或修改了,编译器会提示错误信息。大家可以学习一下为什么我们总是应该在覆盖方法时使用Java覆盖注解

2. @Deprecated – 当我们想要让编译器知道一个方法已经被弃用(deprecate)时,应该使用这个注解。Java推荐在javadoc中提供信息,告知用户为什么这个方法被弃用了,以及替代方法是什么。

3. @SuppressWarnings – 这个注解仅仅是告知编译器,忽略它们产生了特殊警告,比如:在java泛型中使用原始类型。它的保持性策略(retention policy)是SOURCE,在编译器中将被丢弃。

java注解解析

使用Java反射机制从一个类中解析注解,请记住,注解保持性策略应该是RUNTIME,否则它的信息在运行期无效,我们也不能从中获取任何数据。

示例如下;

package com.xxx.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Description("class annotation")
public class LoginController {

@RequestMapping("/login")
@Description(value = "method  annotation", age = 11)
public String login() {

return null;
}

}

----------------------------------------------------------------------------

package com.xxx.controller;

import java.lang.reflect.Method;

public class Test {
public static void main(String[] args) {
try {
// 通过运行时反射API获得annotation信息
Class<?> rtClass = Class.forName("com.xxx.controller.LoginController");
// 判断类是否存在
boolean descriptionExist = rtClass.isAnnotationPresent(Description.class);
if (descriptionExist) {
Description description = (Description) rtClass.getAnnotation(Description.class);
System.out.println("class annotation --- > " + description.value());
// 遍历方法 method annotation
Method[] methods = rtClass.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Description.class)) {
Description author = (Description) method.getAnnotation(Description.class);
System.out.println("method annotation ---> " + author.value());
}
}
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

}

运行结果;

class annotation --- > class annotation
method annotation ---> method annotation



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架允许我们自定义注解,用于标记特定的类、方法或属性,并通过反射来获取或处理这些注解。下面是创建自定义注解的步骤: 1. 创建一个新的Java类,用于定义自定义注解注解需要使用`@interface`关键字进行声明。例如: ```java package com.example.annotations; 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.TYPE) public @interface CustomAnnotation { // 在这里定义注解的属性 String value() default ""; } ``` 在上面的例子中,我们创建了一个名为`CustomAnnotation`的自定义注解,该注解被定义为在运行时可见,并且可以应用于类级别。 2. 在类中使用自定义注解。例如: ```java package com.example; import com.example.annotations.CustomAnnotation; @CustomAnnotation("Hello, world!") public class MyClass { // 类的具体实现 } ``` 在上面的例子中,我们在`MyClass`类上使用了自定义注解`CustomAnnotation`,并传递了一个字符串参数作为注解。 3. 使用反射获取自定义注解信息。例如: ```java package com.example; import com.example.annotations.CustomAnnotation; public class Main { public static void main(String[] args) { Class<?> clazz = MyClass.class; CustomAnnotation annotation = clazz.getAnnotation(CustomAnnotation.class); if (annotation != null) { String value = annotation.value(); System.out.println("Annotation value: " + value); } } } ``` 在上面的例子中,我们使用反射获取`MyClass`类上的`CustomAnnotation`注解,并获取注解。 这就是使用Spring框架自定义注解的基本步骤。你可以根据实际需求在注解中定义不同的属性,并根据需要在应用程序中使用这些自定义注解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值