Java注解当它执行?编译或运行时? [英] Java annotations when does it execute? compile or runtime?

查看:338
本文介绍了Java注解当它执行?编译或运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我受够了手写输入验证逻辑为我所有的服务方法,比如如果参数为空,或者如果列表不是空的,所以......我只是希望写一些注释,可以在运行时执行之前,或立刻一个服务方法被调用后?那么我的注释的知识是非常有限的。我不知道,如果它在运行时或编译时执行。

Well I am fed up of writing input validation logic for all my service method, such as If the argument is null or if the list is not empty so...I am just looking to write some annotation which can execute at runtime, before or immediatly after a service method is invoked? Well my knowledge on annotation is very limited. I dont know if it executes during the runtime or compile time.

推荐答案

注释不执行;他们是由各种工具阅读笔记或标记。有些是由编译器读取,如 @覆盖;其他则是嵌入在类文件并在运行时像Hibernate工具读取。但他们没有做任何自己。

Annotations don't execute; they're notes or markers that are read by various tools. Some are read by your compiler, like @Override; others are embedded in the class files and read by tools like Hibernate at runtime. But they don't do anything themselves.

您可能会想的断言代替,其可用于验证pre和交条件

You might be thinking of assertions instead, which can be used to validate pre and post conditions.

这篇关于Java注解当它执行?编译或运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆