Java反射及依赖注入简单模拟

泪湿孤枕 提交于 2019-12-18 18:46:59

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

一、编写Dao类

package cn.com.songjy.annotation;

import java.util.Date;

public class MyDao {

	public String time(){
		return "你好,现在的时间是:" + new Date();
	}
	
}

二、编写属性文件【my.properties,类似spring的配置文件】并将MyDao类配置到其中,如下:

myDao=cn.com.songjy.annotation.MyDao

三、编写读取属性配置文件的类

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);
	}
}

四、编写注解类

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的默认属性
}

五、反射注入工具类编写

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();
		}
	}
	
}

六、编写测试类

测试类一:

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();
	}
}

测试类二:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!