一、Java还提供了4中注解,专门负责新注解的创建。
@Target | 表示该注解可以用于什么地方,可能的ElementType参数有: CONSTRUCTOR:构造器的声明 FIELD:域声明(包括enum实例) LOCAL_VARIABLE:局部变量声明 METHOD:方法声明 PACKAGE:包声明 PARAMETER:参数声明 TYPE:类、接口(包括注解类型)或enum声明 |
@Retention | 表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括: SOURCE:注解将被编译器丢弃 CLASS:注解在class文件中可用,但会被VM丢弃 RUNTIME:VM将在运行期间保留注解,因此可以通过反射机制读取注解的信息。 |
@Document | 将注解包含在Javadoc中 |
@Inherited | 允许子类继承父类中的注解 |
二、事例
实现了注解的定义、使用注解进行类属性值注入,废话不多说,开始贴代码。
2.1 注解定义
package com.alex.annocation;
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 MyAnnocation {
public String name() default "gaga";
public int age() default 2;
}
2.2 Entity定义
package com.alex.annocation;
public class User {
private String name;
private int age;
public void talk() {
if (this.name.equals("alexZhang")) {
System.out.println("我叫"+this.name+",我今年"+this.age+"了,"+"刘涛,好漂亮,我好喜欢...");
}else if(this.name.equals("liuTao")) {
System.out.println("我叫"+this.name+",我今年"+this.age+"了,"+"alex,你好帅,好像嫁给你...");
}else {
System.out.println("我叫"+this.name+",我今年"+this.age+"了,"+"别说话,让我静静的coding...");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.3 注解使用
package com.alex.annocation;
public class UserFactory {
@MyAnnocation(name="liuTao",age=18)
private User liu;
@MyAnnocation(name="alexZhang",age=18)
private User alex;
@MyAnnocation
private User man;
public User getLiu() {
return liu;
}
public void setLiu(User liu) {
this.liu = liu;
}
public User getAlex() {
return alex;
}
public void setAlex(User alex) {
this.alex = alex;
}
public User getMan() {
return man;
}
public void setMan(User man) {
this.man = man;
}
}
2.4 注解值注入实现
package com.alex.annocation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class DealAnnocation {
private static final UserFactory userFactory;
static {
userFactory = new UserFactory();
}
public static UserFactory getuserFactory() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Field[] fields = userFactory.getClass().getDeclaredFields();
if (fields != null && fields.length > 0 ) {
for (Field field : fields) {
MyAnnocation annotation = field.getAnnotation(MyAnnocation.class);
String name = annotation.name();
int age = annotation.age();
field.setAccessible(true);
Class<?> clazz = (Class<?>)field.getGenericType();
Object object = clazz.newInstance();
if (clazz.getName().equals("com.alex.annocation.User") && annotation != null ) {
field.set(userFactory, object);
}else {
continue;
}
Method[] methods = clazz.getMethods();
if ( methods != null && methods.length > 0 ) {
for (Method umethod : methods) {
if ("setName".equals(umethod.getName())) {
umethod.invoke(object, name);
}
if ("setAge".equals(umethod.getName())) {
umethod.invoke(object, age);
}
}
}
}
}
return userFactory;
}
}
2.5 测试类
package com.alex.annocation;
public class MyMain {
public static void main(String[] args) throws Exception {
User alex = DealAnnocation.getuserFactory().getAlex();
alex.talk();
User liu = DealAnnocation.getuserFactory().getLiu();
liu.talk();
User man = DealAnnocation.getuserFactory().getMan();
man.talk();
}
}
2.6 执行Result
我叫alexZhang,我今年18了,刘涛,好漂亮,我好喜欢...
我叫liuTao,我今年18了,alex,你好帅,好像嫁给你...
我叫gaga,我今年2了,别说话,让我静静的coding...
三、说明
注解的使用很广泛且方便