之前在开发中,就总纳闷,为什么继承接口时,会出现@Override注解,有时候还会提示写注解@SuppressWarnings?
那么什么是注解呢?
注解就是某种注解类型的一个实例,我们可以用它在某个类上进行标注,这样编译器在编译我们的文件时,会根据我们自己设定的方法来编译类。
注解都是什么呢?看下面这张图就明白了!
上面的图可以看出,注解大体上分为三种:标记注解,一般注解,元注解
这里面Override这个没测试出来,因为目前的Eclipse会自动帮我们排错,如果类型不符,是没有办法进行覆盖的。
而Deprecated注解,除了多个删除线,并没有什么拦截功能。
上面的测试,也仅仅是针对IDE,如果是利用javac,应该会有提示的。
其他的不多说了,标准元注解 都是干嘛的呢?
@Documented 标记生成javadoc
@Inherited 标记继承关系
@Retention 注解的生存期
@Target 标注的目标
首先声明一个接口,并未它添加注解内容!
1 package testAnnotation;
2
3 import java.lang.annotation.Documented;
4 import java.lang.annotation.Retention;
5 import java.lang.annotation.RetentionPolicy;
6
7 @Documented
8 @Retention(RetentionPolicy.RUNTIME)
9 public @interface Person{
10 String name();
11 int age();
12 }
然后利用反射机制查看类的注解内容
1 package testAnnotation;
2
3 @Person(name="xingoo",age=25)
4 public class test3 {
5 public static void print(Class c){
6 System.out.println(c.getName());
7
8 //java.lang.Class的getAnnotation方法,如果有注解,则返回注解。否则返回null
9 Person person = (Person)c.getAnnotation(Person.class);
10
11 if(person != null){
12 System.out.println("name:"+person.name()+" age:"+person.age());
13 }else{
14 System.out.println("person unknown!");
15 }
16 }
17 public static void main(String[] args){
18 test3.print(test3.class);
19 }
20 }
运行结果,读取到了注解的内容
testAnnotation.test3 name:xingoo age:25
http://www.cnblogs.com/xing901022/p/3966799.html
前言:这两天看了一下Java自定义注解的内容,然后按照我自己的理解写了两份代码,还挺有趣的,本文包括三个部分:注解的基础、通过注解进行赋值(结合了工厂方法模式)、通过注解进行校验。
一、注解的基础
1.注解的定义:Java文件叫做Annotation,用@interface表示。
2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。
3.注解的保留策略:
@Retention(RetentionPolicy.SOURCE) // 注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
4.注解的作用目标:
@Target(ElementType.TYPE) // 接口、类、枚举、注解
@Target(ElementType.FIELD) // 字段、枚举的常量
@Target(ElementType.METHOD) // 方法
@Target(ElementType.PARAMETER) // 方法参数
@Target(ElementType.CONSTRUCTOR) // 构造函数
@Target(ElementType.LOCAL_VARIABLE) // 局部变量
@Target(ElementType.ANNOTATION_TYPE) // 注解
@Target(ElementType.PACKAGE) // 包
5.注解包含在javadoc中:
@Document
6.注解可以被继承:
@Inherited
7.注解解析器:用来解析自定义注解。
二、通过注解进行赋值(结合了工厂方法模式)
1.自定义注解
package lwp.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Init.java
*
* @author 梁WP 2014年7月10日
*/
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Init
{
public String value() default "";
}
2.在数据模型使用注解
package lwp.model;
import lwp.annotation.Init;
/**
* User.java
*
* @author 梁WP 2014年7月10日
*/
public class User
{
private String name;
private String age;
public String getName()
{
return name;
}
@Init(value = "liang")
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
@Init(value = "23")
public void setAge(String age)
{
this.age = age;
}
}
3.用“构造工厂”充当“注解解析器”
package lwp.factory;
import java.lang.reflect.Method;
import lwp.annotation.Init;
import lwp.model.User;
/**
* UserFactory.java
*
* @author 梁WP 2014年7月10日
*/
public class UserFactory
{
public static User create()
{
User user = new User();
// 获取User类中所有的方法(getDeclaredMethods也行)
Method[] methods = User.class.getMethods();
try
{
for (Method method : methods)
{
// 如果此方法有注解,就把注解里面的数据赋值到user对象
if (method.isAnnotationPresent(Init.class))
{
Init init = method.getAnnotation(Init.class);
method.invoke(user, init.value());
}
}
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return user;
}
}
4.运行的代码
package lwp.app;
import java.lang.reflect.InvocationTargetException;
import lwp.factory.UserFactory;
import lwp.model.User;
/**
* Test.java
*
* @author 梁WP 2014年7月10日
*/
public class Test
{
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException
{
User user = UserFactory.create();
System.out.println(user.getName());
System.out.println(user.getAge());
}
}
5.运行结果
liang 23
三、通过注解进行校验
1.自定义注解
package lwp.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Validate.java
*
* @author 梁WP 2014年7月11日
*/
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Validate
{
public int min() default 1;
public int max() default 10;
public boolean isNotNull() default true;
}
2.在数据模型使用注解
package lwp.model;
import lwp.annotation.Validate;
/**
* User.java
*
* @author 梁WP 2014年7月11日
*/
public class User
{
@Validate(min = 2, max = 5)
private String name;
@Validate(isNotNull = false)
private String age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
}
3.注解解析器
package lwp.check;
import java.lang.reflect.Field;
import lwp.annotation.Validate;
import lwp.model.User;
/**
* UserCheck.java
*
* @author 梁WP 2014年7月11日
*/
public class UserCheck
{
public static boolean check(User user)
{
if (user == null)
{
System.out.println("!!校验对象为空!!");
return false;
}
// 获取User类的所有属性(如果使用getFields,就无法获取到private的属性)
Field[] fields = User.class.getDeclaredFields();
for (Field field : fields)
{
// 如果属性有注解,就进行校验
if (field.isAnnotationPresent(Validate.class))
{
Validate validate = field.getAnnotation(Validate.class);
if (field.getName().equals("age"))
{
if (user.getAge() == null)
{
if (validate.isNotNull())
{
System.out.println("!!年龄可空校验不通过:不可为空!!");
return false;
}
else
{
System.out.println("年龄可空校验通过:可以为空");
continue;
}
}
else
{
System.out.println("年龄可空校验通过");
}
if (user.getAge().length() < validate.min())
{
System.out.println("!!年龄最小长度校验不通过!!");
return false;
}
else
{
System.out.println("年龄最小长度校验通过");
}
if (user.getAge().length() > validate.max())
{
System.out.println("!!年龄最大长度校验不通过!!");
return false;
}
else
{
System.out.println("年龄最大长度校验通过");
}
}
if (field.getName().equals("name"))
{
if (user.getName() == null)
{
if (validate.isNotNull())
{
System.out.println("!!名字可空校验不通过:不可为空!!");
return false;
}
else
{
System.out.println("名字可空校验通过:可以为空");
continue;
}
}
else
{
System.out.println("名字可空校验通过");
}
if (user.getName().length() < validate.min())
{
System.out.println("!!名字最小长度校验不通过!!");
return false;
}
else
{
System.out.println("名字最小长度校验通过");
}
if (user.getName().length() > validate.max())
{
System.out.println("!!名字最大长度校验不通过!!");
return false;
}
else
{
System.out.println("名字最大长度校验通过");
}
}
}
}
return true;
}
}
4.运行的代码
package lwp.app;
import lwp.check.UserCheck;
import lwp.model.User;
/**
* Test.java
*
* @author 梁WP 2014年7月11日
*/
public class Test
{
public static void main(String[] args)
{
User user = new User();
user.setName("liang");
user.setAge("1");
System.out.println(UserCheck.check(user));
}
}
5.运行结果
名字可空校验通过 名字最小长度校验通过 名字最大长度校验通过 年龄可空校验通过 年龄最小长度校验通过 年龄最大长度校验通过 true
http://www.cnblogs.com/liangweiping/p/3837332.html
原文:http://www.cnblogs.com/softidea/p/5533073.html