1. 程式人生 > >Annocation註解的定義及值注入使用

Annocation註解的定義及值注入使用

一、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...

三、說明

    註解的使用很廣泛且方便