简体   繁体   English

纯 Java 中的依赖注入

[英]Dependency Injection in plain Java

I have a large application written in Java SE (JdK 1.7).我有一个用 Java SE (JdK 1.7) 编写的大型应用程序。 But it is getting harder to maintain.但是越来越难维护了。 I thought it would be a nice idea to introduce DI into this project.我认为将 DI 引入这个项目是个好主意。 As long as I have experience with Java EE framework, i would like to know, what is the most common way to implement the dependency injection when one does not use any container like Glassfish or Spring framework.只要我有 Java EE 框架的经验,我想知道,当一个人不使用任何容器(如 Glassfish 或 Spring 框架)时,最常见的实现依赖注入的方法是什么。 I read about Weld and Guice, but have no idea which one is better and is it even "normal" to use such a technique as DI outside a container all on its own.我读过有关 Weld 和 Guice 的文章,但不知道哪个更好,甚至在容器外单独使用诸如 DI 之类的技术是否“正常”。

How can I benefit from IoC in Java SE7 and is it even reasonable?我如何从 Java SE7 中的 IoC 中受益,它甚至合理吗? If it is, which is the library to use for DI in such a plain project?如果是,那么在这样一个普通项目中用于 DI 的库是哪个?

I wanted a similar thing, and decided on guice:我想要一个类似的东西,并决定使用 guice:

Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 5 and above, brought to you by Google. Guice(发音为“juice”)是一个轻量级的 Java 5 及更高版本的依赖注入框架,由 Google 提供给您。 https://code.google.com/p/google-guice/ https://code.google.com/p/google-guice/

It has few dependencies, doesn't need another container/framework to operate, and is well documented.它几乎没有依赖项,不需要另一个容器/框架来运行,并且有很好的文档记录。

Its perfectly normal to use DI outside of a container, and well worth the up front cost.在容器外使用 DI 是完全正常的,并且非常值得前期成本。 I've saved many hours of development time because now I can easily and safely wire up dependencies by following a simple pattern, and creates consistency across the application.我节省了许多小时的开发时间,因为现在我可以通过遵循一个简单的模式轻松安全地连接依赖项,并在整个应用程序中创建一致性。

I've used both Guice (a lot) & Spring (a little).我使用过 Guice(很多)和 Spring(一点)。

Spring is big but modular. Spring 很大但模块化。 This means that if you only want a little you can set it up to only use a little and as all the other modules follow the same patterns used in the core you can pick 'em up easily.这意味着如果你只想要一点,你可以将它设置为只使用一点,因为所有其他模块都遵循核心中使用的相同模式,你可以轻松地选择它们。 However the tutorials etc can meander into Spring at large which can confuse matters.然而,教程等可能会大量进入 Spring,这可能会混淆问题。

Guice is small and targeted. Guice 小而有针对性。 It only contains a little and so is easy to learn.它只包含一点,所以很容易学习。 But offers little help when you want that bit more.但是当您想要更多时,它提供的帮助很小。

IMO Guice is good if you want to get going quickly and don't have anyone with Spring XP on board. IMO Guice 如果您想快速上手并且没有任何人使用 Spring XP,那么它是很好的选择。 While it has less tutorials it is also a lot simpler to get your head around and the tutorials are generally more focused.虽然它的教程较少,但了解它也更简单,而且教程通常更集中。

Spring is good if you already has someone with Spring XP on board or you intend on using more Spring-y goodness in the short term.如果您已经拥有使用 Spring XP 的人,或者您打算在短期内使用更多 Spring-y 优点,那么 Spring 是不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM