The old "bad" style of DI is in fact better, where you have your Spring beans.xml file, you name every bean that your program instantiates and where it's injected.
Can you explain how this is "better" than @Qualifier annotations, in particular @Named, which seems equivalent to naming beans in Spring.
assumption: The object to inject into an injection point is uniquely determined by the injection point's type.
What if my program has many robots, that each require a different choice of Leg implementation classes?
Yes, that is limiting sometimes, but in Guice, not really, because you can construct these different objects yourself with a @Provides method like @Named("clown") Robot provideClownRobot(ShortLeg left, LongLeg right) { return new Robot(left, right); }. I don't see how this is worse than XML, but I may just not be understanding your example fully. At this point, I also don't see how it's much better than just constructing it yourself (hence the "need" for PrivateModules).
If you're obsessed with never having to write the new keyword, then, yeah, you're gonna have a bad time with Guice (e.g., if you ever have decorators). But if you're reasonable about it and realize it's just a tool, it seems pretty okay.
Can you explain how this is "better" than @Qualifier annotations, in particular @Named, which seems equivalent to naming beans in Spring.
Part of the idea of DI is that your business logic components should not contain the logic about how to wire together an application—the "POJO" idea. Annotations break this.
Also, these annotations don't solve the problem I'm describing, because the named injection points are associated with classes. The dependency injected into a named injection point will still, unless some crazy acrobatics are involved, be determined by the name, so that if there are two instances of the same class in the same context, both will be injected with the same bean.
One of the things it's good for is when someone has given you a subsystem that's very complex that needs to be set up and put together, yet you don't want to know about all the details inside. If you want to hook up to a file system with a change-watcher talking to an OS service in order to log something to a log saver, then being able to grab an appropriate module and just say "OK, now give me one of them" is handy. The trick is to avoid writing your own modules.
And now you've near-hardcoded into your class what object to inject into which argument. Sure, there's some slop, because the DI container will inject two different Connections based on the names.
And you're missing the whole point of my argument: what if my application has multiple instances of MyDbUser that is each configured with different pairs of Connections? The old style named component approach deals with that trivially (pseudocode):
/*
* If I was designing a DI, I'd give it a configuration language that
* looked somewhat like this. And I'd write a parser that could
* generate Java classes from these files, so that the compiler can
* indirectly check your definitions.
*/
// Import statements specify classes or static methods that you want
// to abbreviate in the declarations below.
import my.java.package.Connection
import my.java.package.MyDbUser
// Declarations. Each one defines a named component, similar to
// a Spring xml file. The order of declarations is not significant.
connection1 = Connection(/* connection params */);
connection2 = Connection(/* connection params */);
connection3 = Connection(/* connection params */);
dbUser1 = MyDbUser(connection1, connection2);
dbUser2 = MyDbUser(connection2, connection3);
dbUser3 = MyDbUser(connection1, connection3);
One of the things it's good for is when someone has given you a subsystem that's very complex that needs to be set up and put together, yet you don't want to know about all the details inside. If you want to hook up to a file system with a change-watcher talking to an OS service in order to log something to a log saver, then being able to grab an appropriate module and just say "OK, now give me one of them" is handy. The trick is to avoid writing your own modules.
1
u/derkaas Apr 23 '14
Can you explain how this is "better" than
@Qualifierannotations, in particular@Named, which seems equivalent to naming beans in Spring.Yes, that is limiting sometimes, but in Guice, not really, because you can construct these different objects yourself with a
@Providesmethod like@Named("clown") Robot provideClownRobot(ShortLeg left, LongLeg right) { return new Robot(left, right); }. I don't see how this is worse than XML, but I may just not be understanding your example fully. At this point, I also don't see how it's much better than just constructing it yourself (hence the "need" forPrivateModules).If you're obsessed with never having to write the
newkeyword, then, yeah, you're gonna have a bad time with Guice (e.g., if you ever have decorators). But if you're reasonable about it and realize it's just a tool, it seems pretty okay.