请问如何实现“自动的”“dependency injection”?
我使用setter injection 实现dependency injection, 我现在的问题是,如何在不知道TagBeanImpl内部,有什么样静态引用的前提下, "自动地” 将这些静态引用赋值(注射进去)? 谢谢!!!
就好像用java的反射机制对某个类实例,变量赋值,方法调用一样。 我也可以在TagBeanImpl内部用个静态代码块来赋值,但加入第一次没成功咋办,所以总觉得不如在“外部”实现的好,没注入成功可以在来一次。
还有,TagBeanImpl声明的static reference全部是DAO接口,没有其它的。
public class TagBeanImpl implements TagBean {
//
private static TagCoreDao tagCoreDao;
private static TagTreeDao tagTreeDao;
public static TagCoreDao getTagCoreDao() {
return tagCoreDao;
}
public static void setTagCoreDao(TagCoreDao tagCoreDao) {
TagBeanImpl.tagCoreDao = tagCoreDao;
}
public static TagTreeDao getTagTreeDao() {
return tagTreeDao;
}
public static void setTagTreeDao(TagTreeDao tagTreeDao) {
TagBeanImpl.tagTreeDao = tagTreeDao;
}
}