Annotations when it comes to EJB is known as choosing Implicit middle-ware approach over an explicit middle-ware approach , when you use annotation you're customizing what you exactly need from the API
例如,你需要调用银行转帐的交易方法:
不使用注释:
密码是
transfer(Account account1, Account account2, long amount)
{
// 1: Call middleware API to perform a security check
// 2: Call middleware API to start a transaction
// 3: Call middleware API to load rows from the database
// 4: Subtract the balance from one account, add to the other
// 5: Call middleware API to store rows in the database
// 6: Call middleware API to end the transaction
}
而使用注释时,您的代码不包含使用中间-的繁琐 API 调用
代码是干净的,主要关注业务逻辑
transfer(Account account1, Account account2, long amount)
{
// 1: Subtract the balance from one account, add to the other
}
例如,有@Override 注释,它告诉编译器检查此成员函数是否覆盖父类中的一个。还有@Target 注释,它用于指定用户定义的注释(第三种与其他类型的注释没有任何共同之处的构造)可以附加到哪些类型的对象。这些与另一个 except for starting with an @ symbol没有任何关系。
Many of the other answers to this question assume the user is asking about user defined annotations specifically, which are one kind of annotation that defines a set of integers or strings or other data, static to the class or method or variable they are attached to, that can be queried at compile time or run time. Sadly, there is no marker that distinguishes this kind of annotation from other kinds like @interface that do different things.