这篇文章将为大家详细讲解有关Java注解的定义及使用方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
Java的注解在实际项目中使用得非常的多,特别是在使用了Spring之后。
本文会介绍Java注解的语法,以及在Spring中使用注解的例子。
注解的语法
注解的例子
以Junit中的@Test注解为例
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
long timeout() default 0L;
}
可以看到@Test
注解上有@Target()
和@Retention()
两个注解。
这种注解了注解的注解,称之为元注解。
跟声明了数据的数据,称为元数据是一种意思。
之后的注解的格式是
修饰符 @interface 注解名 {
注解元素的声明1
注解元素的声明2
}
注解的元素声明有两种形式
type elementName();
type elementName() default value; // 带默认值
@Target
注解@Target
注解用于限制注解能在哪些项上应用,没有加@Target
的注解可以应用于任何项上。
在java.lang.annotation.ElementType
类中可以看到所有@Target
接受的项
TYPE
在【类、接口、注解】上使用
FIELD
在【字段、枚举常量】上使用
METHOD
在【方法】上使用
PARAMETER
在【参数】上使用
CONSTRUCTOR
在【构造器】上使用
LOCAL_VARIABLE
在【局部变量】上使用
ANNOTATION_TYPE
在【注解】上使用
PACKAGE
在【包】上使用
TYPE_PARAMETER
在【类型参数】上使用 Java 1.8 引入
TYPE_USE
在【任何声明类型的地方】上使用 Java 1.8 引入
@Test
注解只允许在方法上使用。
@Target(ElementType.METHOD)
public @interface Test { ... }
如果要支持多项,则传入多个值。
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface MyAnnotation { ... }
此外元注解也是注解,也符合注解的语法,如@Target
注解。@Target(ElementType.ANNOTATION_TYPE)
表明@Target
注解只能使用在注解上。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Retention
注解@Retention
指定注解应该保留多长时间,默认是RetentionPolicy.CLASS
。
在java.lang.annotation.RetentionPolicy
可看到所有的项
SOURCE
不包含在类文件中
CLASS
包含在类文件中,不载入虚拟机
RUNTIME
包含在类文件中,由虚拟机载入,可以用反射API获取
@Test
注解会载入到虚拟机,可以通过代码获取
@Retention(RetentionPolicy.RUNTIME)
public @interface Test { ... }
@Documented
注解主要用于归档工具识别。被注解的元素能被Javadoc
或类似的工具文档化。
@Inherited
注解添加了@Inherited
注解的注解,所注解的类的子类也将拥有这个注解
注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation { ... }
父类
@MyAnnotation
class Parent { ... }
子类Child
会把加在Parent
上的@MyAnnotation
继承下来
class Child extends Parent { ... }
@Repeatable
注解Java 1.8 引入的注解,标识注解是可重复使用的。
注解1
public @interface MyAnnotations {
MyAnnotation[] value();
}
注解2
@Repeatable(MyAnnotations.class)
public @interface MyAnnotation {
int value();
}
有使用@Repeatable()
时的使用
@MyAnnotation(1)
@MyAnnotation(2)
@MyAnnotation(3)
public class MyTest { ... }
没使用@Repeatable()
时的使用,@MyAnnotation
去掉@Repeatable
元注解
@MyAnnotations({
@MyAnnotation(1),
@MyAnnotation(2),
@MyAnnotation(3)})
public class MyTest { ... }
这个注解还是非常有用的,让我们的代码变得简洁不少,Spring
的@ComponentScan
注解也用到这个元注解。
8种基本数据类型(byte
,short
,char
,int
,long
,float
,double
,boolean
)
String
Class
enum
注解类型
数组(所有上边类型的数组)
枚举类
public enum Status {
GOOD,
BAD
}
注解1
@Target(ElementType.ANNOTATION_TYPE)
public @interface MyAnnotation1 {
int val();
}
注解2
@Target(ElementType.TYPE)
public @interface MyAnnotation2 {
boolean boo() default false;
Class<?> cla() default Void.class;
Status enu() default Status.GOOD;
MyAnnotation1 anno() default @MyAnnotation1(val = 1);
String[] arr();
}
使用时,无默认值的元素必须传值
@MyAnnotation2(
cla = String.class,
enu = Status.BAD,
anno = @MyAnnotation1(val = 2),
arr = {"a", "b"})
public class MyTest { ... }
Java
内置的注解@Override
注解告诉编译器这个是个覆盖父类的方法。如果父类删除了该方法,则子类会报错。
@Deprecated
注解表示被注解的元素已被弃用。
@SuppressWarnings
注解告诉编译器忽略警告。
@FunctionalInterface
注解Java 1.8 引入的注解。该注释会强制编译器javac
检查一个接口是否符合函数接口的标准。
有两种比较特别的注解
标记注解 : 注解中没有任何元素,使用时直接是 @XxxAnnotation
, 不需要加括号
单值注解 : 注解只有一个元素,且名字为value
,使用时直接传值,不需要指定元素名@XxxAnnotation(100)
Java
的AnnotatedElement
接口中有getAnnotation()
等获取注解的方法。
而Method
,Field
,Class
,Package
等类均实现了这个接口,因此均有获取注解的能力。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface MyAnno {
String value();
}
@MyAnno("class")
public class MyClass {
@MyAnno("feild")
private String str;
@MyAnno("method")
public void method() { }
}
public class Test {
public static void main(String[] args) throws Exception {
MyClass obj = new MyClass();
Class<?> clazz = obj.getClass();
// 获取对象上的注解
MyAnno anno = clazz.getAnnotation(MyAnno.class);
System.out.println(anno.value());
// 获取属性上的注解
Field field = clazz.getDeclaredField("str");
anno = field.getAnnotation(MyAnno.class);
System.out.println(anno.value());
// 获取方法上的注解
Method method = clazz.getMethod("method");
anno = method.getAnnotation(MyAnno.class);
System.out.println(anno.value());
}
}
Spring
中使用自定义注解注解本身不会有任何的作用,需要有其他代码或工具的支持才有用。
设想现有这样的需求,程序需要接收不同的命令CMD
,
然后根据命令调用不同的处理类Handler
。
很容易就会想到用Map
来存储命令和处理类的映射关系。
由于项目可能是多个成员共同开发,不同成员实现各自负责的命令的处理逻辑。
因此希望开发成员只关注Handler
的实现,不需要主动去Map
中注册CMD
和Handler
的映射。
最终希望看到效果是这样的
@CmdMapping(Cmd.LOGIN)
public class LoginHandler implements ICmdHandler {
@Override
public void handle() {
System.out.println("handle login request");
}
}
@CmdMapping(Cmd.LOGOUT)
public class LogoutHandler implements ICmdHandler {
@Override
public void handle() {
System.out.println("handle logout request");
}
}
开发人员增加自己的Handler
,只需要创建新的类并注上@CmdMapping(Cmd.Xxx)
即可。
具体的实现是使用Spring
和一个自定义的注解
定义@CmdMapping
注解
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface CmdMapping {
int value();
}
@CmdMapping
中有一个int
类型的元素value
,用于指定CMD
。这里做成一个单值注解。
这里还加了Spring
的@Component
注解,因此注解了@CmdMapping
的类也会被Spring创建实例。
然后是CMD
接口,存储命令。
public interface Cmd {
int REGISTER = 1;
int LOGIN = 2;
int LOGOUT = 3;
}
之后是处理类接口,现实情况接口会复杂得多,这里简化了。
public interface ICmdHandler {
void handle();
}
上边说过,注解本身是不起作用的,需要其他的支持。下边就是让注解生效的部分了。
使用时调用handle()
方法即可。
@Component
public class HandlerDispatcherServlet implements
InitializingBean, ApplicationContextAware {
private ApplicationContext context;
private Map<Integer, ICmdHandler> handlers = new HashMap<>();
public void handle(int cmd) {
handlers.get(cmd).handle();
}
public void afterPropertiesSet() {
String[] beanNames = this.context.getBeanNamesForType(Object.class);
for (String beanName : beanNames) {
if (ScopedProxyUtils.isScopedTarget(beanName)) {
continue;
}
Class<?> beanType = this.context.getType(beanName);
if (beanType != null) {
CmdMapping annotation = AnnotatedElementUtils.findMergedAnnotation(
beanType, CmdMapping.class);
if(annotation != null) {
handlers.put(annotation.value(), (ICmdHandler) context.getBean(beanType));
}
}
}
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
}
主要工作都是Spring
做,这里只是将实例化后的对象put
到Map
中。
测试代码
@ComponentScan("pers.custom.annotation")
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(Main.class);
HandlerDispatcherServlet servlet = context.getBean(HandlerDispatcherServlet.class);
servlet.handle(Cmd.REGISTER);
servlet.handle(Cmd.LOGIN);
servlet.handle(Cmd.LOGOUT);
context.close();
}
}
关于Java注解的定义及使用方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。