What is dependancy Injection?

Hi,I read a lot about 'Dependancy Injection' in Java EE 5, Spring etc.Can some one tell me what this is? Are there more injections?Thanks in advance,Rajeev.
[193 byte] By [Rajeev.Asthanaa] at [2007-10-2 22:08:41]
# 1

> I read a lot about 'Dependancy Injection' in Java EE 5, Spring etc.

>

> Can some one tell me what this is? Are there more injections?

Yes, when you get bitten by a little old scabby dog you get tetanus

injections at the hospital and a load of antibiotics ;-)

Seriously though: if an object needs an implementation of an interface I

as one of its members it should traditionally take care of it itself, either

by instantiating another object from some class that implements that

interface I or by consulting a factory that will do the job for it.

DI is the other way around, i.e. you declaratively tell that object that any

implementation of that interface I can be found somewhere as some

class. Your object either needs an appropriate setter or a constructor

that can deal with this dependency.

This way you don't need to change any compiled code at all, you just

change the descriptor for that particular bean/class.

kind regards,

Jos

JosAHa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 2
Can I have some example....
Rajeev.Asthanaa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 3
look here: http://www.springframework.organd read this: http://www.martinfowler.com/articles/injection.html%
duffymoa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 4

here's interface injection:

public class Foo

{

private Bar bar;

public void setBar(Bar b) { this.bar = b; }

}

Class Foo depends on a Bar instance. Set it using the setBar method.

Here's constructor injection:

public class Foo

{

private Bar bar;

public Foo(Bar b) { this.bar = b; }

}

Class Foo depends on a Bar instance. Set it using the Foo constructor.

Dependency injection frameworks make it possible for you to do this using declarative syntax rather than hard-wiring in your source code.

%

duffymoa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 5
Sorry, should have called that "setter injection". More coffee, please.%
duffymoa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 6
> Can I have some example....Check out Springs' online documentation. They've got nice simple examples to start with.kind regards,Jos
JosAHa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 7
It's going over my head. Can someone describe it more clearly. Why it's called Injection and what exactly is dependancy injection?Thanks
Rajeev.Asthanaa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 8

>It's going over my head. Can someone describe it more clearly.

If you cannot understand the Martin Fowler article, nobody can help you.

>Why it's called Injection

Injection \In*jec"tion\, n. [L. injectio : cf. F. injection.]

1. The act of injecting or throwing in; -- applied

particularly to the forcible insertion of a liquid or gas,

by means of a syringe, pump, etc.

[1913 Webster]

Ex. Rajeev.Asthana needs an injection of magic comprehension serum.

IanSchneidera at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 9
> Sorry, should have called that "setter injection".That's the little scabby dog that Jos was talking about?
DrClapa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 10

> > Sorry, should have called that "setter injection".

>

> That's the little scabby dog that Jos was talking about?

Pa, "Old Setter" is dead! He's dead! They've killed my scabby little dog!

To the OP: Gotta help yourself here. Do some reading.

"Dependency" means that class A needs an instance of class B to do its work. A depends on B.

Still with me? Too fast?

"Injection" because you have to give class A a copy of class B. You can do that by both of the means that I showed you earlier. You can understand both of those, right? That's just Java - no framework involved.

Am I writing slowly enough for you?

The new wrinkle is that frameworks such as Spring let you spell out those dependencies declaratively in configuration files and save you from having to write a lot of plumbing code to do the injection.

%

duffymoa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...
# 11

> > Sorry, should have called that "setter injection".

>

> That's the little scabby dog that Jos was talking about?

No that would've been 'b a s t a r d injection' then. The bit was just a little

scratch on the knuckle that didn't even hurt; the tetanus injection and a

load of antibiotics made me feel like a wet old newspaper for more

than a week though ;-)

kind regards,

Jos

JosAHa at 2007-7-14 1:25:27 > top of Java-index,Java Essentials,New To Java...