Friday, December 7, 2007

multiple injection(single point injection)

Lucy enables multiple injection.

What is multiple injection?
Multiple injection is the way to inject components by one method.
Usually, IoC container supports one component injection by one method.

Look like this:


@Component
@Prototype
public class Client2 {

protected Randomizer randomizer;

public void execute() {
System.out.println(randomizer.getRandom());
}

@Inject
public void setRandomizer(Randomizer randomizer) {
this.randomizer = randomizer;
}
}



This is not bad, but when the situation to inject so many components,
it is not so good to see.


So, I made like this:


@Component
@Prototype
public class Client3 {

protected Add1 a1;

protected Add2 a2;

public int execute() {
return a1.get() + a2.get();
}

@Inject
public void setAll(Add1 a1, Add2 a2) {
this.a1 = a1;
this.a2 = a2;
}
}



This gives you single point injection,
which I prefer to use.

Almost all the IoC container looks like they recommend JavaBeans way,
one component injection per method, but it is skeptical to me it is better way.
If I would see Client2 and Client3 first, I would love to see latter one,
because I can see verrry well where Add1 and Add2 to be injected(are we saying it is visualization?).

No comments: