简体   繁体   English

Google Guice与PicoContainer的依赖注入

[英]Google Guice vs. PicoContainer for Dependency Injection

My team is researching dependency injection frameworks and is trying to decide between using Google-Guice and PicoContainer. 我的团队正在研究依赖注入框架,并试图决定使用Google-Guice和PicoContainer。

We are looking for several things in our framework: 我们正在寻找框架中的几件事:

  1. A small code footprint - What I mean by a small code footprint is we don't want to have dependency injection code litter everywhere in our code base. 代码占用空间小 - 我的意思是代码占用空间很小,我们不希望代码库中的依赖注入代码丢失。 If we need to refactor down the road, we want it to be as easy as possible. 如果我们需要在路上进行重构,我们希望它尽可能简单。
  2. Performance - How much overhead does each framework have when creating and injecting objects? 性能 - 创建和注入对象时每个框架有多少开销?
  3. Ease of use - Is there a large learning curve? 易于使用 - 是否有很大的学习曲线? Do we have to write mounds of code to get something simple working? 我们是否必须编写大量代码才能使简单的工作变得简单? We want to have as little configuration as possible. 我们想要尽可能少的配置。
  4. Community size - Larger communities usually means that a project will continue to be maintained. 社区规模 - 较大的社区通常意味着将继续维护项目。 We don't want to use a framework and have to fix our own bugs ;) Also any questions we have along the way can (hopefully) be answered by the framework's developer/user community . 我们不想使用框架并且必须修复我们自己的错误;)我们在此过程中遇到的任何问题都可以(希望)由框架的开发人员/用户社区来回答。

Comparisons of the two frameworks against the listed criteria would be greatly appreciated. 将非常感谢两个框架与所列标准的比较。 Any personal experiences that help to compare the two would also be extremely helpful. 任何有助于比较两者的个人经历也会非常有帮助。

Disclaimer: I'm fairly new to dependency injection so excuse my noob-ness if I asked a question that isn't pertinent to this discussion. 免责声明:我对依赖注入相当新,如果我问一个与本次讨论无关的问题,请原谅我的新闻。

You may want to include Spring in your list of Dependency Injection frameworks you are considering. 您可能希望将Spring包含在您正在考虑的依赖注入框架列表中。 Here are some answers to your questions: 以下是您的问题的一些答案:

Coupling to the framework 耦合到框架

Pico - Pico tends to discourage setter injection but other than that, your classes don't need to know about Pico. Pico - Pico倾向于阻止二传手注射,但除此之外,你的课程不需要了解Pico。 It's only the wiring that needs to know (true for all DI frameworks). 它只是需要知道的布线(对于所有DI框架都是如此)。

Guice - Guice now supports the standard JSR 330 annotations, so you do not need Guice specific annotations in your code anymore. Guice - Guice现在支持标准的JSR 330注释,因此您不再需要在代码中使用Guice特定的注释。 Spring also supports these standard annotations. Spring还支持这些标准注释。 The argument that the Guice guys use is that without a Guice annotation processor running, these shouldn't have an impact if you decide to use a different framework. Guice家伙使用的论点是,如果没有运行Guice注释处理器,如果您决定使用不同的框架,这些应该不会产生影响。

Spring - Spring aims to allow you to avoid any mention of the Spring framework in your code. Spring - Spring旨在让您避免在代码中提及Spring框架。 Because they do have a lot of other helpers / utilities etc. the temptation is pretty strong to depend on Spring code, though. 因为它们确实有很多其他帮助程序/实用程序等,但依赖于Spring代码的诱惑非常强烈。

Performance 性能

Pico - I'm not too familiar with the speed characteristics of Pico Pico - 我不太熟悉Pico的速度特性

Guice - Guice was designed to be fast and the comparison mentioned in the reference has some numbers. Guice - Guice设计得很快,参考文献中提到的比较有一些数字。 Certainly if speed is a primary consideration either using Guice or wiring by hand should be considered 当然,如果速度是主要考虑因素,则应考虑使用Guice或手工布线

Spring - Spring can be slow. 春天 - 春天可能很慢。 There has been work to make it faster and using the JavaConfig library should speed things up. 已经有工作使其更快,并且使用JavaConfig库可以加快速度。

Ease of use 便于使用

Pico - Simple to configure. Pico - 配置简单。 Pico can make some autowire decisions for you. Pico可以为您做出一些自动装配决定。 Not clear how it scales to very large projects. 不清楚它如何扩展到非常大的项目。

Guice - Simple to configure, you just add annotations and inherit from AbstractModule to bind things together. Guice - 配置简单,只需添加注释并从AbstractModule继承即可将事物绑定在一起。 Scales well to large projects as configuration is kept to a minimum. 随着配置保持最小,可以很好地扩展到大型项目。

Spring - Relatively easy to configure but most examples use Spring XML as the method for configuration. Spring - 相对容易配置,但大多数示例使用Spring XML作为配置方法。 Spring XML files can become very large and complex over time and take time to load. 随着时间的推移,Spring XML文件会变得非常庞大和复杂,并且需要花时间加载。 Consider using a mix of Spring and hand cranked Dependency Injection to overcome this. 考虑使用Spring和手摇依赖注入的混合来克服这个问题。

Community Size 社区规模

Pico - Small 皮科 - 小

Guice - Medium Guice - 中等

Spring - Large 春天 - 大

Experience 经验

Pico - I haven't had much experience with Pico but it is not a widely used framework so it will be harder finding resources. Pico - 我对Pico没有多少经验,但它不是一个广泛使用的框架,因此更难找到资源。

Guice - Guice is a popular framework and its focus on speed is welcome when you've got a large project that you're restarting a lot in development. Guice - Guice是一个流行的框架,当你有一个大型项目在开发中重新开始时,它对速度的关注是受欢迎的。 I have a concern about the distributed nature of the configuration ie it's not easy to see how our whole application is put together. 我对配置的分布式特性感到担忧,即不容易看出整个应用程序是如何组合在一起的。 It's a bit like AOP in this respect. 在这方面,它有点像AOP。

Spring - Spring is usually my default choice. Spring - Spring通常是我的默认选择。 That said, the XML can become cumbersome and the resulting slowdown annoying. 也就是说,XML可能会变得很麻烦,导致的减速很烦人。 I often end up using a combination of hand crafted Dependency Injection and Spring. 我经常最终使用手工制作的依赖注入和Spring的组合。 When you actually need XML based configuration, Spring XML is quite good. 当您真正需要基于XML的配置时,Spring XML非常好。 Spring also put a lot of effort into making other frameworks more Dependency Injection friendly which can be useful because they often use best practice when doing so (JMS, ORM, OXM, MVC etc.). Spring也花费了很多精力来使其他框架更加依赖于Dependency Injection,这可能很有用,因为他们在这样做时经常使用最佳实践(JMS,ORM,OXM,MVC等)。

References 参考

The answer put up by jamie.mccrindle is actually pretty good, but I'm left confused why Spring is the default choice when it's pretty clear that superior alternatives (both Pico and Guice) are available. jamie.mccrindle提出的答案实际上相当不错,但我很遗憾为什么Spring是默认选择,因为很明显可以使用优质替代品(Pico和Guice)。 IMO Spring's popularity has reached it's peak and now it's currently living off the generated hype (along with all the other "me too" Spring sub projects looking to ride the Spring bandwagon). IMO Spring的受欢迎程度达到了它的最高点,现在它已经过了生成的炒作(以及所有其他“我也是”Spring子项目希望乘坐Spring的潮流)。

Spring's only real advantage is community size (and quite frankly, due to the size and complexity, it's needed), but Pico and Guice don't need a huge community because their solution is much cleaner, more organized, and more elegant. Spring的唯一真正优势是社区规模(坦率地说,由于规模和复杂性,它是必需的),但Pico和Guice 不需要庞大的社区,因为他们的解决方案更清洁,更有条理,更优雅。 Pico seems more flexible than Guice (you can use annotations in Pico, or not--it's extremely efficient). Pico似乎比Guice更灵活(你可以在Pico中使用注释,或者不是 - 它非常有效)。 (Edit: Meant to say it's extremely flexible, not that it isn't also efficient.) (编辑:意思是说它非常灵活,并不是说效率也不高。)

Pico's tiny size and lack of dependencies is a MAJOR win which shouldn't be understated. Pico的小尺寸和缺乏依赖性是一个不可低估的主要胜利。 How many megs do you need to download to use Spring now? 你现在需要下载多少个meg才能使用Spring? It's a kludgy-mess of huge jar files, with all it's dependencies. 这是一个巨大的jar文件,包含所有依赖项。 Intuitively thinking, such an efficient and "small" solution should scale and perform better than something like Spring. 直观地思考,这样一个高效且“小”的解决方案应该比Spring更好地扩展和执行。 Is Spring's bloat really going to make it scale better? Spring的臃肿真的会让它更好地扩展吗? Is this bizarro world? 这个奇异的世界吗? I wouldn't make assumptions that Spring is "more scalable" until that's proven (and explained). 在证明(并解释)之前,我不会假设Spring“更具可扩展性”。

Sometimes creating something good (Pico/Guice) and then keeping your HANDS OFF of it instead of adding bloat and kitchen sink features with endless new versions really does work out... 有时创造一些好东西(Pico / Guice),然后保持你的HANDS关闭而不是添加膨胀和厨房水槽功能与无尽的新版本真的确实有效...

NOTE: This is more of a comment/rant than an answer 注意:这更像是评论/咆哮而不是答案

PicoContainer is great. PicoContainer很棒。 I'd switch back to it if they'd just fix their web sites. 如果他们只是修复他们的网站,我会切​​换回它。 It's really confusing now: 现在真的很混乱:

I'm using Guice 2.x now, even though it's bigger, and it has fewer features. 我现在正在使用Guice 2.x,即使它更大,但功能更少。 It was just much easier to find the documentation, and it's user group is very active. 查找文档要容易得多,而且用户组非常活跃。 However, if the direction of Guice 3 is any indication, it looks like Guice is starting to bloat, just like Spring did way back in the early days. 然而,如果Guice 3的方向是任何迹象,看起来Guice开始膨胀,就像Spring在早期做的那样。

Update: I posted a comment to the Pico Container folks and they've made some improvements to the web site. 更新:我向Pico Container人员发表了评论,他们对网站进行了一些改进。 Much better now! 现在好多了!

It is a old question but today you can consider Dagger ( https://github.com/square/dagger ) in your Android App project. 这是一个老问题,但今天您可以在Android App项目中考虑Dagger( https://github.com/square/dagger )。 Dagger does code generation on compilation time. Dagger在编译时执行代码生成。 So you get a shorter startup time and less memory usage on execution time. 因此,您可以缩短启动时间,减少执行时的内存使用量。

If you're after a minimalistic DI container, you can check out Feather . 如果你正在使用简约的DI容器,你可以查看羽毛 Vanilla JSR-330 DI functionality only, but quite good in terms of footprint (16K, no dependencies) and performance. 仅限Vanilla JSR-330 DI功能,但在占用空间(16K,无依赖性)和性能方面相当不错。 Works on android. 适用于android。

Although I do like PicoContainer for it's simplicity and it's lack of dependencies. 虽然我喜欢PicoContainer,但它的简单性和缺乏依赖性。 I would recommend using CDI instead because it is part of the Java EE standard so you have no vendor lock-in. 我建议使用CDI,因为它是Java EE标准的一部分,因此您没有供应商锁定。

In terms of intrusiveness, it's main problem is the requirement of a container and the use of a relatively empty META-INF/beans.xml file (needed to indicate that the jar is using CDI) and the use of annotations (though they are standard) 就侵入性而言,它的主要问题是容器的需求和使用相对空的META-INF / beans.xml文件(需要表明jar使用CDI)和注释的使用(尽管它们是标准的) )

The lightweight CDI container I use for my own projects is Apache Open Web Beans. 我用于自己项目的轻量级CDI容器是Apache Open Web Beans。 Though it took a while to figure out how to create a simple app (unlike Pico) which looks like this. 虽然花了一些时间来弄清楚如何创建一个看起来像这样的简单应用程序(不像Pico)。

public static void main(final String[] args) {
    final ContainerLifecycle lifecycle = WebBeansContext.currentInstance()
            .getService(ContainerLifecycle.class);
    lifecycle.startApplication(null);

    final BeanManager beanManager = lifecycle.getBeanManager();
    // replace Tester with your start up class
    final Bean<?> bean = beanManager.getBeans(Tester.class).iterator()
            .next();

    final Tester b = (Tester) lifecycle.getBeanManager().getReference(bean,
            Tester.class, beanManager.createCreationalContext(bean));
    b.doInit();
}

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

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