Java 注解 Dependency injection
前言
Linus Benedict Torvalds : RTFSC – Read The Fucking Source Code
现有的开源库
RoboGuice
Android Annotations
Dragger
ButterKnife
Transfuse
注解(Annotation)
从三个方向了解注解:
注解是什么?
注解有什么用?
注解怎么用?
注解是什么
注解(Annotation),也叫元数据。一种代码级别的说明,是一种将程序中的元素与任何信息关联一起的途径和方法。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。
注解有什么用
创建文档。
跟踪代码中的依赖性。
甚至执行基本编译时检查。
在Java 5.0 中java.lang包有三个标准Annotation类型。
标准 Annotation (JDK内置系统注解)
标准 Annotation 是指 Java 自带的几个 Annotation,例如: @Override, @Deprecated, @SuppressWarnings。
元 Annotation (元注解)
元 Annotation 是指用来定义 Annotation 的 Annotation,例如:@Retention, @Target, @Inherited, @Documented。
自定义 Annotation (自定义注解)
自定义 Annotation 表示自己根据需要定义的 Annotation,定义时需要用到元 Annotation。
注解怎么用
注解的语法比较简单,就是在前面添加@符号,后面的语法就更Java语法一致。
标准 Annotation
我们列出三个例子:
@Override
@Deprecated
@SuppressWarnnings
@Override
用于修饰此方法覆盖了父类的方法。
@Override 是一个标记注解类型,它被用作标注方法。它说明了被标注的方法重载了父类的方法,起到了断言的作用。如果重载的方法写错了在编译期会有警告出现。
@Deprecated
用于修饰已经过时的方法。
@Deprecated是一个标记注解。当一个类型或者类型成员使用@Deprecated修饰的话,编译器将不鼓励使用这个被标注的程序元素。这个标记注解具有继承性,可被子类继承。
@SuppressWarnnings
用于通知java编译器禁止特定的编译警告。
@SuppressWarnings 被用于有选择的关闭编译器对类、方法、成员变量、变量初始化的警告。@SuppressWarnings使用格式有这几种:
@SuppressWarnings(value={"deprecation", "unchecked"})
@SuppressWarnings({"deprecation", "unchecked"})
@SuppressWarnings("unchecked")
常见的@SuppressWarnings参数值:
all to suppress all warnings
boxing to suppress warnings relative to boxing/unboxing operations
cast to suppress warnings relative to cast operations
dep-ann to suppress warnings relative to deprecated annotation
deprecation to suppress warnings relative to deprecation
fallthrough to suppress warnings relative to missing breaks in switch statements
finally to suppress warnings relative to finally block that don’t return
hiding to suppress warnings relative to locals that hide variable
incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
nls to suppress warnings relative to non-nls string literals
null to suppress warnings relative to null analysis
rawtypes to suppress warnings relative to un-specific types when using generics on class params
restriction to suppress warnings relative to usage of discouraged or forbidden references
serial to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access to suppress warnings relative to incorrect static access
synthetic-access to suppress warnings relative to unoptimized access from inner classes
unchecked to suppress warnings relative to unchecked operations
unqualified-field-access to suppress warnings relative to field access unqualified
unused to suppress warnings relative to unused code
元 Annotation
元注解的作用就是负责注解其他注解。在Java 5.0 中有四个标准元注解:
@Target
@Retention
@Documented
@Inherited
@Target
定义了Annotation所修饰的对象范围:可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。
取值类型有这么几个:
CONSTRUCTOR:用于描述构造器
FIELD:用于描述域
LOCAL_VARIABLE:用于描述局部变量
METHOD:用于描述方法
PACKAGE:用于描述包
PARAMETER:用于描述参数
TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention
定义了该Annotation被保留的时间长短。
取值类型有这么几个:
SOURCE:在源文件中有效(即源文件保留)
CLASS:在class文件中有效(即class保留)
RUNTIME:在运行时有效(即运行时保留)
@Documented
用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被如javadoc此类的工具文档化。
@Inherited
这是一个标记注解,阐述了某个被标注的类型是被继承的。
自定义 Annotation
通过使用@interface符号来完成自定义注解,自定义注解不能继承其它注解或接口。可以通过default来声明参数的默认值。
注解格式:
public @interface 注解名 {定义体}
支持的数据类型:
所有基本数据类型(int,float,boolean,byte,double,char,long,short)
String类型
Class类型
enum类型
Annotation类型
以上所有类型的数组
备注:
如果只有一个参数成员,最好把参数名称设为"value",后加小括号。
只能用public或默认(default)这两个访问权修饰。
例子:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileName {
String value() default "";
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileType {
public enum Color{BULE,RED,GREEN};
Color fruitColor() default Color.GREEN;
}
// 使用
@FileName("Safe")
private String FileName;
注解处理器类库
最后这里介绍下注解处理类库:
java.lang.reflect.AnnotatedElement
可以通过它处理很多复杂的注解工作。
注解学习图