Java反射及依賴注入簡單模擬

1、編寫Dao類 java

package cn.com.songjy.annotation;

import java.util.Date;

public class MyDao {

	public String time(){
		return "你好,如今的時間是:" + new Date();
	}
	
}

2、編寫屬性文件【my.properties,相似spring的配置文件】並將MyDao類配置到其中,以下: spring

myDao=cn.com.songjy.annotation.MyDao

3、編寫讀取屬性配置文件的類 工具

package cn.com.songjy.annotation;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {

	private static InputStream in = PropertiesUtil.class.getClassLoader()
			.getResourceAsStream("my.properties");

	public PropertiesUtil() {
	}

	private static Properties props = new Properties();

	static {
		try {
			props.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getValue(String key) {
		return props.getProperty(key);
	}

	public static void updateProperties(String key, String value) {
		props.setProperty(key, value);
	}
}

4、編寫註解類 測試

package cn.com.songjy.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyDiAnnotation {

	//public String name();
	
	String value() default "";//value是Annotation的默認屬性
}

5、反射注入工具類編寫 this

package cn.com.songjy.annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyAnnotationUtil {
	
	MyAnnotationUtil(){
		di(this);
	}

	public static void di(Object obj){
		
		try {
			
			Method[] methods = obj.getClass().getDeclaredMethods();
			
			for (Method method : methods) {
				
				if(method.isAnnotationPresent(MyDiAnnotation.class)){//只處理包含MyDiAnnotation的方法
					
					MyDiAnnotation diAnnotation = method.getAnnotation(MyDiAnnotation.class);
					
					String class_key = diAnnotation.value();
					
					if(null==class_key || class_key.trim().length()==0){//key值默認爲setXXX中的XXX且第一個字母要轉換爲小寫
						class_key = method.getName().substring(3);
						class_key = class_key.substring(0, 1).toLowerCase() + class_key.substring(1);
					}
					
					method.invoke(obj, Class.forName(PropertiesUtil.getValue(class_key)).newInstance());
					
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
}

6、編寫測試類 code

測試類一: get

package cn.com.songjy.annotation;

public class MyService01 extends MyAnnotationUtil {

	MyDao myDao;
	
	public MyDao getMyDao() {
		return myDao;
	}

	@MyDiAnnotation
	public void setMyDao(MyDao myDao) {
		this.myDao = myDao;
	}

	public void hello(){
		System.out.println(myDao.time());
	}
	
	public static void main(String[] args) {
		new MyService01().hello();
	}
}

測試類二: string

package cn.com.songjy.annotation;

public class MyService02 extends MyAnnotationUtil {

	MyDao dao;
	
	public MyDao getDao() {
		return dao;
	}

	@MyDiAnnotation("myDao")
	public void setDao(MyDao dao) {
		this.dao = dao;
	}

	public void hello(){
		System.out.println(dao.time());
	}
	
	public static void main(String[] args) {
		new MyService02().hello();
	}
}

 

輸出結果以下:你好,如今的時間是:Sat Jun 29 17:02:03 CST 2013 it

相關文章
相關標籤/搜索