可能的重复:
Java 中如何以及在何处使用注解?
Java beans,注释:它们有什么作用做?他们如何帮助我?
我一遍又一遍地了解到 Java 5 的注释是该语言的“高级功能”。直到最近,我还没有太多使用注释(除了通常的 @Override 和 &c),但是一些与 Web 服务相关的项目的工作迫使我不得不动手。自从我学习 Java pre-5 以来,我从未真正花时间坐下来研究注释系统。
我的问题 - 你们真的使用注释吗?它们对您的日常帮助有多大?有多少 StackOverflow 用户必须编写自定义注释?
Possible Duplicates:
How and where are Annotations used in Java?
Java beans, annotations: What do they do? How do they help me?
Over and over, I read about Java 5's annotations being an 'advanced feature' of the language. Until recently, I haven't much used annotations (other than the usual @Override, &c), but work on a number of webservice-related projects has forced my hand. Since I learned Java pre-5, I never really took the time to sit down and grok the annotation system.
My question- do you guys actually use annotations? How helpful are they to you, day-to-day? How many StackOverflow-ers have had to write a custom annotation?
发布评论
评论(9)
也许Java注释最有用和最常用的情况是使用POJO +注释而不是xml配置文件
我经常使用它,因为(正如你已经说过的)如果你使用Web框架(如spring或seam),他们通常有很多注释来帮助你。
我最近编写了一些注释来构建自定义状态机、验证目的和注释的注释(使用它的元数据方面)。在我看来,它们对使代码更清晰、更易于理解和管理有很大帮助。
Perhaps the most useful and used case of Java Annotations is to use POJO + Annotation instead of xml configuration files
I use it a lot since (as you already stated) if you use a web framework (like spring or seam) they usually have plenty of annotations to help you.
I have recently wrote some annotations to build a custom statemachine, validations purpose and annotations of annotations (using the metadata aspect of it). And IMO they help a lot making the code cleaner, easier to understand and manage.
目前的项目(200KLOC),我一直使用的注释是:
但我还没有写我自己的注释......还没有!
Current project (200KLOC), annotations I use all the time are:
But I haven't written yet my own annotation... Yet!
我已经使用了注释:
我为以下内容创建了注释:
当我需要获取有关我正在使用的对象的更多信息时,我创建的注释将通过反射来读取。它有效,而且效果很好。
I have used annotations for:
I have created annotations for:
The annotations that I have created are read with Reflection when I need to get more information about the object I am working with. It works and it works great.
注释仅适用于框架,它们在 hibernate/jpa 中工作得很好。除非您编写一个需要从传递给它的对象中获取一些额外信息的框架,否则您不会编写自己的注释。
然而,有一个新的很酷的 junit 功能可以让您在测试中编写自己的注释 - http://blog.mycila.com/2009/11/writing-your-own-junit-extensions-using.html
Annotations are just for frameworks and they do work great in hibernate/jpa. until you write a framework that needs some extra information from passed to it objects you wont write your own annotations.
however there is new and cool junit feature that let you write your own annotations in tests - http://blog.mycila.com/2009/11/writing-your-own-junit-extensions-using.html
我每天都使用注释,它们非常棒。我将它们与 jsf 和 jpa 一起使用,发现它们比替代 XML 配置更容易管理和使用。
I use annotations daily and they are wonderful. I use them with jsf and jpa and find them much easier to manage and work with than the alternative XML configurations.
我在我的状态同步系统中使用注释来描述哪些类是注释类的特化,以及它们应该使用的环境(当创建一个对象时,它将计算出其实体列表,哪些是最好的实体类)为网络上的节点创建;即服务器节点的 Player 实体改为 ServerPlayer 实体)。此外,还描述了类内的属性以及它们应如何跨机器同步。
I use annotations for describing in my state synchronisation system what classes are specialisations of the annotated classes, and the environment in which they should be used (when an object is created, it will work out for its entity lists which are the best entity classes to create for the nodes on the network; i.e., a Player entity for a server node is instead a ServerPlayer entity). Additionally, the attributes inside the classes are described and how they should be synchronised across machines.
我们只是使用注释创建了一种简单的方法来验证 POJO:
然后我们通过 Hibernate 验证器 将为我们完成所有验证:
效果很好。漂亮干净的代码。
We just used annotations to create a simple way to validate our POJO's:
Then we run this through the Hibernate validator which will do all our validation for us:
Works great. Nice clean code.
我们使用自定义注释作为集成测试系统的一部分:
@Artifact:将集成测试与问题 ID 相关联。然后为我们的测试和监管部门自动生成跟踪矩阵。
@Exclude:忽略基于浏览器平台/版本的集成测试。防止 IE 6 错误阻塞我们的夜间测试运行:)
@SeleniumSession:为每个集成测试定义测试特定的 selenium 设置。
它们是非常强大的工具,但您必须小心使用它们。只要看一下那些早期的 .NET Enterprise 类文件,就会知道强制注释是多么可怕的事情:)
We use custom annotations as a part of our integration testing system:
@Artifact: Associates an integration test with an issue ID. Trace matrices are then automatically generated for our testing and regulatory departments.
@Exclude: Ignores an integration test based on the browser platform / version. Keeps the IE 6 bugs from clogging up our nightly test runs :)
@SeleniumSession: Defines test specific selenium settings for each integration test.
They are a very powerful tool, but you gotta use them carefully. Just have a look at those early .NET Enterprise class files to see what a nightmare mandatory annotations can be :)
我们有一个报告生成器作为我们网络应用程序的一部分。用户可以添加大量小部件,这些小部件都是同一组主题(图表、表格等)的小变体。
UI 根据小部件类中的自定义注释自行构建。 (例如,注释可能包含默认值和将呈现为下拉列表的有效值。或者指示该字段是否为必填的标志)。
事实证明,这是一种让开发人员无需接触 UI 即可制作小部件的好方法。
We have a report builder as part of our webapp. A user can add a large number of widgets that are all small variations on the same set of themes (graphs, tables, etc).
The UI builds itself based on custom annotations in the widget classes. (e.g. an annotation might contain default value and valid values that would render as a dropdown. Or a flag indicating if the field is mandatory).
It has turned out be be a good way to allow devs to crank out widgets without having to touch the UI.