http://blog.csdn.net/heyutao007/article/details/5981555
元程序元素关联任何信息和任何元数据的方法。
注解的分类
按运行机制分:
源码注解
编译时注解:JDK注解
运行时注解:@Autowired等
按照来源分:
JDK注解
第三方注解
自定义注解
元注解:注解的注解
JDK自带注解
@Override
@Deprecated
@Suppresswarnings忽略警告
第三方注解
Spring:
@Autowired
对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
对成员变量使用 @Autowired来消除 set ,get方法。Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。
对构造函数用:@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。
对方法用:@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。
@Service
@Repository
自定义注解
- @interface
- 无参数,无异常抛出的方法
- 成员类型受限:原始类型、String、Class、Annotation、Enumeration
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description{
String desc();
String author();
int age() default 18;
}
ElementType.TYPE:作用域是类或接口
FIELD: 成员变量
- 使用时
@Description("the name method")
public String name(){
return null;
}
@Description(value="the name method",name="key")
public String name(){
return null;
}
注解的解析
通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。