开发者

Roboguice @Inject service confusion

开发者 https://www.devze.com 2023-03-16 03:20 出处:网络
private Service service; @Inject public ClassName(fi开发者_JAVA百科nal Service service) { this.service = service;
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

精彩评论

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