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

一、编写Dao类

1
2
3
4
5
6
7
8
9
10
11
package cn.com.songjy.annotation;
 
import java.util.Date;
 
public class MyDao {
 
    public String time(){
        return "你好,现在的时间是:" + new Date();
    }
     
}

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

1
myDao=cn.com.songjy.annotation.MyDao

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
    }
}

四、编写注解类

1
2
3
4
5
6
7
8
9
10
11
12
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的默认属性
}

五、反射注入工具类编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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();
        }
    }
     
}

六、编写测试类

测试类一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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();
    }
}

测试类二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

上一篇:在 iOS 10.0 之后, App 要调用手机相机与相簿应注意的事项


下一篇:jquery全选框的实现