This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Features new in Java 7 and the fly likes Dependency Injection in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "Dependency Injection in Java" Watch "Dependency Injection in Java" New topic
Author

Dependency Injection in Java

Shiv Swaminathan
Ranch Hand

Joined: Jul 21, 2010
Posts: 48

I checked the Table of Contents for "The Well Grounded Java Developer".

Chapter 3 is on Dependency Injection.

Some basic questions:

1. Is the DI in Java similar to what Spring framework offers? Is it a remake?
2. Does Java just provide the Spec for DI and we need some vendor implementation for this spec to utilize DI?
3. How can I make my current Java code to utilize Java DI?
4. Do I necessarily need to use Guice API's to achieve Java DI?
5. Do we have any other implementation available for Java DI other than Guice?


SCEA EE5, CSM, PMP, IBM OOAD with UML, SCDJWS, SCWCD, SCJP 1.5
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30405
    
150

1) It is a standard inspired by Spring (and more)
2) I believe you need some sort of DI container.
3) Look for things you'd like to decouple. For example, when writing test code, it is common to inject an implementation of an interface.
4) No
5) Spring and PicoContainer.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Shiv Swaminathan
Ranch Hand

Joined: Jul 21, 2010
Posts: 48

Spring framework on DI was there before Java came up with any DI specs.
How can it be that Spring is implementing java DI?
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

This is a very common pattern is software. Good ideas become design patterns, design patterns are bult into frameworks (like Spring) and then into a language itself (the JSR-330 std for Java). Spring 3.x simply implements the common JSR-330 std + some extras.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30405
    
150

Shiv,
That's how JPA came about too.

1) Hibernate started.
2) JPA spec came out
3) Hibernate provided set of APIs to be compliant with JPA spec
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dependency Injection in Java