Java 任何人都可以清楚地解释为什么 Google Guice 有用吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1462640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can anyone provide a clear explanation of why Google Guice is useful?
提问by sanity
I've read about Google Guice, and understand the general issues with other approaches to dependency injection, however I haven't yet seen an example of someone using Guice "in practice" where its value becomes clear.
我已经阅读了 Google Guice,并了解了其他依赖注入方法的一般问题,但是我还没有看到有人在“实践中”使用 Guice 的例子,它的价值变得清晰。
I'm wondering if anyone is aware of any such examples?
我想知道是否有人知道任何这样的例子?
采纳答案by Joshua Partogi
Using Google Guice to provides ease in unit testing is only high-level advantage. Some people might not even use unit testing in their project. People has been using Spring/Dependency Injection more than only for unit testing.
使用 Google Guice 来简化单元测试只是高级别的优势。有些人甚至可能不会在他们的项目中使用单元测试。人们不仅将 Spring/Dependency Injection 用于单元测试。
The low level advantage of using Google Guice is a matter of cohesion in your application, your classes in the project can be loosely coupled between each other. I can provide a class for another class without them being dependent to each other.
使用 Google Guice 的低级优势是您的应用程序的内聚问题,您在项目中的类可以彼此松散耦合。我可以为另一个班级提供一个班级,而不会相互依赖。
Consider this example:
考虑这个例子:
public class A{
}
public class B{
A a = new A();
}
Class B would be tightly coupled to Class A, or in other words it is dependent to class A's existence.
B 类将与 A 类紧密耦合,或者换句话说,它依赖于 A 类的存在。
But with Guice I can instead make it loosely coupled like this:
但是有了 Guice,我可以让它像这样松散耦合:
public class B{
private A a;
@Inject
public B(A a){
this.a = a;
}
}
Class B is now loosely coupled to A, and Guice is responsible to provide the instance of A instead of B having to instantiate it. With this you can extend it to provide interface of A to B, and the implementation can be a Mock object if you want to unit test your apps.
B 类现在与 A 松散耦合,Guice 负责提供 A 的实例,而不是 B 必须实例化它。有了这个,你可以扩展它以提供 A 到 B 的接口,如果你想对你的应用程序进行单元测试,实现可以是一个 Mock 对象。
Having said that we're only discussing the benefits of Dependency Injection so far. Beyond Dependency Injection, the benefits of using Google Guice is:
话虽如此,我们目前只讨论依赖注入的好处。除了依赖注入,使用 Google Guice 的好处是:
- Guice has a very clean implementation of constructor Injection. As you can see from the example you just add @Inject annotation constructor.
- Guice also has setter Injection using the same annotation.
- Having said that, the annotation based Injection is very clean approach compared to XML based injection like some other DI implementation.
- All of the dependency injection and configuration is using Java, so you are guaranteed to get a typesafety in your application by default.
- Guice has a very lightweight implementation of Aspect Oriented Programming (or maybe you can call it as a wrapper to the AOPAlliance AOP implementation). And the good thing of it is it doesn't generate stubs or what-so-ever.
- Guice 有一个非常干净的构造函数注入实现。正如您从示例中看到的,您只需添加 @Inject 注释构造函数。
- Guice 也有使用相同注解的 setter 注入。
- 话虽如此,与其他一些 DI 实现等基于 XML 的注入相比,基于注解的注入是非常干净的方法。
- 所有的依赖注入和配置都使用 Java,所以你可以保证在默认情况下在你的应用程序中获得类型安全。
- Guice 有一个非常轻量级的面向方面编程的实现(或者您可以将它称为 AOPAlliance AOP 实现的包装器)。它的好处是它不会生成存根或任何东西。
That's the overview of it. But as you get deeper with Guice, there's so many more good things about it. A simplereal life example is if you are using GWT with MVP implementation, the components/widgets in your GWT application are very loosely coupled and not tightly integrated to each other.
这就是它的概述。但是随着您对 Guice 的深入了解,它还有更多的好处。一个简单的现实生活示例是,如果您将GWT 与 MVP 实现一起使用,则 GWT 应用程序中的组件/小部件非常松散耦合,彼此之间没有紧密集成。
回答by duffymo
I think the advantage comes with coding to interfaces, testing, and proxies.
我认为优势在于对接口、测试和代理进行编码。
Coding to an interface helps keep your code layered properly, makes it possible to inject mocks for testing, and lets you generate proxies automagically so client code need not worry about implementation.
对接口进行编码有助于保持您的代码正确分层,可以注入模拟进行测试,并让您自动生成代理,因此客户端代码无需担心实现。
This is true for Guice, Spring, PicoContainer, and all DI frameworks.
这适用于 Guice、Spring、PicoContainer 和所有 DI 框架。
Succinct enough?
够简洁吗?
回答by Pascal Thivent
Maybe you should go back in time and look closer at the problems Guice wanted to solve. To understand the motivations behind Guice, the Bob Lee: I Don't Get Springnews on TheServerSide.COM (and its comments) is the perfect starting point. Then, go ahead with the announcement of Google Guice, A Java Dependency Injection Framework(and the comments) and the Tech Talk: Bob Lee on Google Guice(and the comments).
也许你应该回到过去,仔细看看 Guice 想要解决的问题。要了解 Guice 背后的动机,Bob Lee: I Don't Get Springnews on TheServerSide.COM(及其评论)是完美的起点。然后,继续发布Google Guice、Java 依赖注入框架(和评论)和技术讲座:Bob Lee on Google Guice(和评论)。
Personally, I was sharing concerns about evil XML: XML configuration hell, XML and possible runtime errors, error-prone and refactoring-adverse string identifiers, etc, etc. Actually, I believe that skeptical opinions on Spring and concurrence were good for everybody (including Spring). I was thus happy to see a new player in the DI framework landscape, especially a modern framework leveraging Java 5 features (generics and annotations for the sake of type safety).
就我个人而言,我分享了对邪恶 XML 的担忧:XML 配置地狱、XML 和可能的运行时错误、容易出错和重构不利的字符串标识符等。实际上,我相信对 Spring 和并发的怀疑意见对每个人都有好处(包括春天)。因此,我很高兴看到 DI 框架领域的新参与者,尤其是利用 Java 5 特性(为了类型安全而使用泛型和注释)的现代框架。
And because Google is running Guice in mission critical applications (almost every Java-based application is also a Guice-base application: AdWords, Google Docs, Gmail, and even YouTube as reported by "Crazy" Bob Lee in Guice2), I can't believe Guice is totally wrong and doesn't provide any value. Sadly, I don't think Google will provide much code of these applications as examples... But you may find interesting things in the list of applications that use Guiceand/or the list of 3rd party Guice addons. Or check out the books mentioned in Guice2. Or ask Bob :)
而且因为 Google 在关键任务应用程序中运行 Guice(几乎每个基于 Java 的应用程序也是基于 Guice 的应用程序:AdWords、Google Docs、Gmail,甚至 YouTube,正如Guice2 中“Crazy”Bob Lee 所报告的那样),我不能不要相信 Guice 是完全错误的并且不提供任何价值。遗憾的是,我认为 Google 不会提供这些应用程序的大量代码作为示例……但是您可能会在使用 Guice 的应用程序列表和/或3rd party Guice addons 列表中发现有趣的事情。或者查看Guice2 中提到的书籍。或者问鲍勃:)