使用过ssh框架的人一定也使用过注解,尤其是在spring框架中,注解可谓是spring容器和AOP编程的重要环节。注解就是用于修饰类、全局变量、方法、参数或局部变量的接口,java中规定,注解的使用不允许影响其修饰类的存在,也就是说如果将一个类的注解全部删除,这个类也能够编译通过。java中,注解的应用主要有四个方面:类注解,全局变量注解,方法注解,参数注解。其他的还有诸如包注解和局部变量注解这里暂不作讨论。首先我们定义一个类注解如下:

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
String say() default "this is a type annotation";
}

在java中有四个元注解,分别是@RetentIon、@Target、@Inherited和@Document,@Retention指定的是注解的生命周期,其value参数的值有三个:RUNTIME、SOURCE、CLASS。RUNTIME表示当前修饰的注解是在虚拟机运行时实时加载的,也就是虚拟机中要用到该注解一次就加载一次;CLASS表示该注解在类的源文件和编译后的class文件中都存在,而在类加载器加载到虚拟机中时就会消失,SOURCE表示该注解只在源文件中存在,当编译成class文件后就不存在了。@Target指定的是当前注解可修饰的类型,可value参数可取如下几个值:Type、FIELD、METHOD、PARAMETER、PACKAGE、CONSTRUCTOR、LOCAL_VARIABLE、ANNOTATION_TYPE等。其中最主要的应用有TYPE、FIELD、METHOD、PARAMETER,其分别表示类注解,全局变量注解,方法注解,参数注解,其余几个则表示包注解,构造器注解,局部变量注解,用于注解的注解等。

注解的声明同接口的声明类似,不过要在interface关键字前加@修饰以标明其为一个注解,在使用注解是括号中的变量名称也即注解声明时方法名称,声明方法时可以默认指定该方法的默认值,即在使用注解时若不指定该“变量”则该“变量”使用该方法的默认值,这里需要说明的是,若方法名称为value,那么在使用注解时,注解中该“变量名”可以不写,如上述Retention注解中的值(这种写法要求使用该注解时只指定这一个方“变量”的值)。

如下示例我们分别演示了四个常用注解的声明方式:

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldAnnotation {
String say() default "this is a field annotation";
}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
String say() default "this is a method annotation";
}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface ParamAnnotation {
String say() default "this is a parameter annotation";
}

由于注解的使用已经非常广泛,下面我们通过一个类来简单演示注解的使用:

package source;

import annotation.FieldAnnotation;
import annotation.MethodAnnotation;
import annotation.ParamAnnotation;
import annotation.TypeAnnotation; @TypeAnnotation(say = "this is modified type annotation")
public class Apple {
@FieldAnnotation(say = "this is modified field annotation")
private String color; @MethodAnnotation(say = "this is modified method annotation")
public void setColor(@ParamAnnotation String color) {
this.color = color;
} public String getColor() {
return color;
}
}

由于java规定,注解的使用不能对其使用类的存在产生影响,因而注解对于使用类只能起到一定的辅助作用,一个非常好的应用实例为javaweb中的权限控制,我们可以预定义一些权限码,然后在需要权限校验的地方使用自定义的注解;不过,因为java支持反射,因而通过反射注解就可以与使用类非常好的配合在一起,从而实现某些功能,一个非常好的例子是spring中的实体类对象属性的注入。下面我们通过一个简单的例子来说明如何通过反射获取注解值,并且利用反射使其与使用类产生关系:

package test;

import annotation.FieldAnnotation;
import annotation.MethodAnnotation;
import annotation.ParamAnnotation;
import annotation.TypeAnnotation;
import source.Apple; import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type; import org.junit.Test; public class App { @Test
public void testApp() throws Exception { Apple apple = Apple.class.newInstance(); // 获取被注释的类
if (Apple.class.isAnnotationPresent(TypeAnnotation.class)) {
System.out.println(Apple.class.getAnnotation(TypeAnnotation.class).say());
} // 获取被注释的变量
Field[] fields = Apple.class.getDeclaredFields();
for (Field f : fields) {
if (f.isAnnotationPresent(FieldAnnotation.class)) {
System.out.println(f.getAnnotation(FieldAnnotation.class).say());
}
} // 获取被注释的方法
Method[] methods = Apple.class.getDeclaredMethods();
for (Method m : methods) {
if (m.isAnnotationPresent(MethodAnnotation.class)) {
System.out.println(m.getAnnotation(MethodAnnotation.class).say());
} // 获取当前方法被注释的参数
Parameter[] parameters = m.getParameters();
for (Parameter p : parameters) {
if (p.isAnnotationPresent(ParamAnnotation.class)) {
System.out.println(p.getAnnotation(ParamAnnotation.class).say());
}
} // 通过反射调用方法
if (m.getName().startsWith("set")) {
m.invoke(apple, "Red");
}
} System.out.println("通过反射调用对象的方法后对象变量的值:" + apple.getColor());
}
}

运行结果如下:

this is modified type annotation
this is modified field annotation
this is modified method annotation
this is a parameter annotation
通过反射调用对象的方法后对象变量的值:Red

上述示例简单的阐述了反射和注解之间紧密的联系。在实际应用中, 注解的使用非常广泛,通过注解可以大大简化我们的开发,并且实现某些特定的效果,本文对注解的创建,使用以及深层调用方式进行了简单的介绍,希望大家能够喜欢!

java中注解的使用的更多相关文章

  1. 沉淀再出发:java中注解的本质和使用

    沉淀再出发:java中注解的本质和使用 一.前言 以前XML是各大框架的青睐者,它以松耦合的方式完成了框架中几乎所有的配置,但是随着项目越来越庞大,XML的内容也越来越复杂,维护成本变高.于是就有人提 ...

  2. 关于Java中注解的总结

    注解用来给类声明附加额外信息,可以标注在类.字段.方法等上面,编译器.JVM以及开发人员等都可以通过反射拿到注解信息,进而做一些相关处理 Java中注解的知识结构图 常用注解 @Override   ...

  3. java中注解的使用与实例 (二)

    java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单.注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后 某个时刻方便地使用这些数据(通过 解 ...

  4. java中注解的使用与实例(一)

    注解目前非常的流行,很多主流框架都支持注解,而且自己编写代码的时候也会尽量的去用注解,一时方便,而是代码更加简洁. 注解的语法比较简单,除了@符号的使用之外,它基本与Java固有语法一致.Java S ...

  5. 【转】java中注解的使用与实例

    原文:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单. ...

  6. Java中注解Annotation的定义、使用、解析

    此例子,用于说明如何在Java中对“注解 Annotation”的定义.使用和解析的操作.注解一般用于自定义开发框架中,至于为什么使用,此处不作过多说明,这里只说明如何使用,以作备记.下面例子已测试, ...

  7. [转] java中注解的使用与实例

    注解目前非常的流行,很多主流框架都支持注解,而且自己编写代码的时候也会尽量的去用注解,一时方便,而是代码更加简洁. 注解的语法比较简单,除了@符号的使用之外,它基本与Java固有语法一致.Java S ...

  8. java中注解的使用与实例

    1.spring中的一段代码: @Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @ ...

  9. JAVA中注解的实现原理

    注解的本质 「java.lang.annotation.Annotation」接口中有这么一句话,用来描述『注解』. The common interface extended by all anno ...

随机推荐

  1. 大数据学习(6)MapReduce应用

    倒排索引 /** * * * <pre> *file1.txt: *hello ketty *hello tomcat * *file2.txt: *hello hadoop * *map ...

  2. SQL Server 全文索引的管理

    全文索引不同于常见的聚集索引或非聚集索引,这些索引的内部实现是平衡树(B-Tree)结构,而全文索引在物理上是由一系列的内部表(Internal tables)构成的,这些内部表称作全文索引片段(Fr ...

  3. 抓包工具 fiddler

    1. 抓包软件 TCP 外挂: 1. 解包工具  2.抓包工具 HTTP 1.前后端交互过程 2.学习的作用 3.模拟网络情况 2 http client server 没有加密 https 证书 非 ...

  4. Bandit Wargame Level12 Writeup

    Level Goal The password for the next level is stored in the file data.txt, which is a hexdump of a f ...

  5. 记一次诡异的jetty问题

    问题出现 用eclipse开发,用jetty跑某个项目时,如果是jsp页面,会出现以下错误. ------------------------------------------------ java ...

  6. Visual Studio 实用技能

    1快捷键使用 1. Ctrl K F 代码对齐

  7. iOS UIAlertController在Tableview中显示缓慢,迟钝,延迟

    在UITableViewCell中弹窗Alert延迟.在cellForRow中:cell.selectionStyle = UITableViewCellSelectionStyleNone; 或者在 ...

  8. Slf4j与其他日志系统兼容的使用

    java生产的各种框架(如spring等)里各个框架会使用不同的日志体系,多个不同日志在一个jvm里混搭会出现一定问题 ,这里梳理一下java体系里常见的日志框架,以SFL4j为中心介绍下跟各个日志框 ...

  9. Java并发编程之ThreadLocal源码分析

    ## 1 一句话概括ThreadLocal<font face="微软雅黑" size=4>  什么是ThreadLocal?顾名思义:线程本地变量,它为每个使用该对象 ...

  10. Java 哲学家进餐

    某次操作系统实验存档.V 这个哲学家除了吃就知道睡.( ╯□╰ ) 哲学家.java: package operating.entity.philosophyeating; import operat ...