谷歌 Guice 和 PicoContainer 的依赖注入

我的团队正在研究依赖注入框架,并试图在使用 Google-Guice 和 PicoContainer 之间做出选择。

我们正在我们的框架中寻找以下几点:

  1. 少量的代码占用——我所说的少量代码占用是指我们不想让依赖注入代码在我们的代码库中随处可见。如果我们需要在未来进行重构,我们希望它尽可能简单。
  2. 性能——在创建和注入对象时,每个框架有多少开销?
  3. 易用性-是否有较大的学习曲线?我们必须编写成堆的代码才能让一些简单的东西工作吗?我们希望有尽可能少的配置。
  4. 社区规模——较大的社区通常意味着项目将继续维护。我们不想使用一个框架并且必须修复我们自己的 bug;)同样,我们在这个过程中遇到的任何问题(希望)都可以由框架的开发者/用户社区来回答。

如能将这两个框架与所列标准进行比较,将不胜感激。任何有助于比较这两者的个人经历也会非常有帮助。

免责声明: 我是新依赖注入,所以如果我问了一个与本次讨论无关的问题,请原谅我的菜鸟态度。

33737 次浏览

You may want to include Spring in your list of Dependency Injection frameworks you are considering. 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. It's only the wiring that needs to know (true for all DI frameworks).

Guice - Guice now supports the standard JSR 330 annotations, so you do not need Guice specific annotations in your code anymore. Spring also supports these standard annotations. 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.

Spring - Spring aims to allow you to avoid any mention of the Spring framework in your code. Because they do have a lot of other helpers / utilities etc. the temptation is pretty strong to depend on Spring code, though.

Performance

Pico - I'm not too familiar with the speed characteristics of Pico

Guice - Guice was designed to be fast and the comparison mentioned in the reference has some numbers. Certainly if speed is a primary consideration either using Guice or wiring by hand should be considered

Spring - Spring can be slow. There has been work to make it faster and using the JavaConfig library should speed things up.

Ease of use

Pico - Simple to configure. Pico can make some autowire decisions for you. 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. 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 XML files can become very large and complex over time and take time to load. Consider using a mix of Spring and hand cranked Dependency Injection to overcome this.

Community Size

Pico - Small

Guice - Medium

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.

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. I have a concern about the distributed nature of the configuration i.e. it's not easy to see how our whole application is put together. It's a bit like AOP in this respect.

Spring - Spring is usually my default choice. That said, the XML can become cumbersome and the resulting slowdown annoying. I often end up using a combination of hand crafted Dependency Injection and Spring. When you actually need XML based configuration, Spring XML is quite good. 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.).

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. 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).

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. Pico seems more flexible than Guice (you can use annotations in Pico, or not--it's extremely efficient). (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. How many megs do you need to download to use Spring now? It's a kludgy-mess of huge jar files, with all it's dependencies. Intuitively thinking, such an efficient and "small" solution should scale and perform better than something like Spring. Is Spring's bloat really going to make it scale better? Is this bizarro world? I wouldn't make assumptions that Spring is "more scalable" until that's proven (and explained).

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...

NOTE: This is more of a comment/rant than an answer

PicoContainer is great. 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. 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.

Update: I posted a comment to the Pico Container folks and they've made some improvements to the web site. Much better now!

Although I do like PicoContainer for it's simplicity and it's lack of dependencies. I would recommend using CDI instead because it is part of the Java EE standard so you have no vendor lock-in.

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)

The lightweight CDI container I use for my own projects is Apache Open Web Beans. Though it took a while to figure out how to create a simple app (unlike Pico) which looks like this.

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();
}

It is a old question but today you can consider Dagger (https://github.com/square/dagger) in your Android App project. Dagger does code generation on compilation time. 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. Vanilla JSR-330 DI functionality only, but quite good in terms of footprint (16K, no dependencies) and performance. Works on android.