由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Dependency Injection
相关主题
Spring Framework magicj2se和j2ee的差别具体在哪?
请问最佳的方案:在其他项目基础上扩展怎么能学成J2EE的全能
spring frame work question请大家看看选哪个工作
找工作时Spring面试一般问题会怎么问?请推荐一本Web-Based Applications with java的书
Instability?Spring 工作机会好象不多啊!
java 依赖注入和反射是必须掌握的吗?Ruby on Rails hype
the best way to transfer data?我发现J2EE的工作对英语要求比较高
Any good book recommendation for EJB?[合集] java方面的工作工资都比较高些
相关话题的讨论汇总
话题: dependency话题: injection话题: service话题: client话题: subsystem
进入Java版参与讨论
1 (共1页)
t*********e
发帖数: 630
1
Wikipedia 谈到 dependency injenction 的缺点:
1. Dependency injection can make code difficult to trace (read) because it
separates behavior from construction. This means developers must refer to
more files to follow how a system performs.[citation needed]
2. Dependency injection typically requires more lines of code to accomplish
the same behavior legacy code would.[citation needed]
3. Dependency injection diminishes encapsulation by requiring users of a
system to know how it works and not merely what it does.[5]
4. Dependency injection increases coupling by requiring the user of a
subsystem to provide for the needs of that subsystem.[6]
1 & 2 没有问题。3 & 4 是什么情况?怎么感觉 DI 不要求客户了解 dependency 的细
节?
On the contrary, it increases encapsulation and decreases coupling?
t*********e
发帖数: 630
2
后面接着讲到 injection 的三种方式,constructor injection, setter injection
and interface injection. For instance,
Class Client {
private Service service;

public void setService(Service service) {
this.service = service;
}
}
Or
Class Client {

private Service service;

public Client(Service service) {
this.service = service
}
}
在 JEE 里面, 好像一行就解决了. 不用什么 constructor 或 setter method.
Class Client {
@Inject
private Service service;
}
怎么跟 wiki 上面说的不一样?

accomplish

【在 t*********e 的大作中提到】
: Wikipedia 谈到 dependency injenction 的缺点:
: 1. Dependency injection can make code difficult to trace (read) because it
: separates behavior from construction. This means developers must refer to
: more files to follow how a system performs.[citation needed]
: 2. Dependency injection typically requires more lines of code to accomplish
: the same behavior legacy code would.[citation needed]
: 3. Dependency injection diminishes encapsulation by requiring users of a
: system to know how it works and not merely what it does.[5]
: 4. Dependency injection increases coupling by requiring the user of a
: subsystem to provide for the needs of that subsystem.[6]

z****e
发帖数: 54598
3
第一种和第二种都是不用reflection
并用xml配置的
第三种是直接annotation,实现用了reflection
reflection可以直接访问私有变量
然后塞入reference
以前没有annotation的时候,第三种需要写到xml里面去
这中间有一个历史发展的过程,所以不同时期都不太一样
不过越来越简单倒是没错,所以你用最后一种就好了
t*********e
发帖数: 630
4
谢谢。
DI 减少了很多 new 的使用,挺好的。

【在 z****e 的大作中提到】
: 第一种和第二种都是不用reflection
: 并用xml配置的
: 第三种是直接annotation,实现用了reflection
: reflection可以直接访问私有变量
: 然后塞入reference
: 以前没有annotation的时候,第三种需要写到xml里面去
: 这中间有一个历史发展的过程,所以不同时期都不太一样
: 不过越来越简单倒是没错,所以你用最后一种就好了

1 (共1页)
进入Java版参与讨论
相关主题
[合集] java方面的工作工资都比较高些Instability?
Web framework comparisonjava 依赖注入和反射是必须掌握的吗?
spring/hibernate/ajax/web 2.0/bpel booksthe best way to transfer data?
Java在Web领域是不是太笨重了?Any good book recommendation for EJB?
Spring Framework magicj2se和j2ee的差别具体在哪?
请问最佳的方案:在其他项目基础上扩展怎么能学成J2EE的全能
spring frame work question请大家看看选哪个工作
找工作时Spring面试一般问题会怎么问?请推荐一本Web-Based Applications with java的书
相关话题的讨论汇总
话题: dependency话题: injection话题: service话题: client话题: subsystem