开发者

Roboguice @Inject service confusion

private Service service;

@Inject
public ClassName(fi开发者_JAVA百科nal Service service) {
    this.service = service;
}

@Inject
private Service service;

Could someone please tell me the difference between this 2 styles of injection?


The first one says that the injector will call your constructor and provide the appropriate Service object. What you do in the constructor is up to you.

The second one says that it will set your member variable appropriately (meaning you could have a default constructor, and it would still magically set the variable for you).

Generally, I prefer the first, as it keeps your dependencies explicit, but I am sure there are good reasons for using method 2.

I haven't used RoboGuice, but I assume it has the same general principles as regular Guice - and some relevant @Inject documentation for that is at their wiki: http://code.google.com/p/google-guice/wiki/Injections

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜