RetentionPolicy.CLASS: 编译器将把注解记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解. 这是默认值
RetentionPolicy.RUNTIME:编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注解. 程序可以通过反射获取该注释
RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注释
(3)@Target:指定注解用于修饰类的哪个成员. @Target 包含了一个名为 value,类型为ElementType的成员变量。
(4)@Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被 javadoc 工具提取成文档.
(5)@Inherited: 被它修饰的 Annotation 将具有继承性.如果某个类使用了被 @Inherited 修饰的 Annotation, 则其子类将自动具有该注解
package com.itheima.annotation.app1;
public class UserDaoImplTest {
private UserDaoImpl dao = new UserDaoImpl();
/**
* 测试添加操作
*/
@MyTest
public void testAddUser() {
dao.addUser();
}
@MyTest
public void testDelUser() {
dao.delUser();
}
}
package com.itheima.annotation.app1;
public class UserDaoImpl {
public void addUser(){
System.out.println("addUser");
}
public void delUser(){
System.out.println("delUser");
}
}
package com.itheima.annotation.app1;
import java.lang.annotation.Documented;
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})
@Documented
public @interface MyTest {
}
public class MyTestRunner {
public static void main(String[] args) throws Exception {
runTest(UserDaoImplTest.class);
}
/**
* 执行测试
* @param clazz测试类的的字节码
* @throws Exception
*/
public static void runTest(Class clazz) throws Exception{
//只有@MyTest注解的方法才是测试方法
//得到测试类中的所有方法 Tips:Class Method Field Constructor都实现了AnnotatedElement接口
Method ms[] = clazz.getMethods();
//遍历:看看谁的上面有@MyTest
for(Method m:ms){
//谁有就执行谁
boolean b = m.isAnnotationPresent(MyTest.class);
if(b)
m.invoke(clazz.newInstance(), null);
}
}
}
package com.itheima.annotation.app2;
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 Limit {
float value();
}
package com.itheima.annotation.app2;
public class Client {
public static void main(String[] args) throws Exception {
Account a = new Account(1000000);
a.drawMoney(3000);
}
}
package com.itheima.annotation.app2;
import java.lang.reflect.Method;
public class Account {
private float amount;//余额
public Account(float amount){//开户
this.amount = amount;
}
//取款
@Limit(4000)
public void drawMoney(float money) throws Exception{
//先判断余额足吗
if(money>amount)
throw new RuntimeException("余额不足");
//用传统方式
//ResourceBundle rb = ResourceBundle.getBundle("com.itheima.annotation.app2.cfg");
//String sLimit = rb.getString("limit");
//用注册方式
Class clazz = Account.class;
Method m = clazz.getMethod("drawMoney", float.class);//得到取款这个方法
Limit limitAnno = m.getAnnotation(Limit.class);//得到方法上的Limit注解
float limit = limitAnno.value();//取属性的值
//判断一次取款是否超限:2K 3K
//float limit = Float.parseFloat(sLimit);
if(money>limit)
throw new RuntimeException("一次取款不能超过"+limit);
amount = amount-money;
System.out.println("您本次取款:"+money+",余额是:"+amount);
}
}
25、提取 Annotation 信息
(1)JDK 5.0 在 java.lang.reflect 包下新增了 AnnotationElement 接口, 该接口代表程序中可以接受注释的程序元素
(2)当一个