The final formis interface injection, which is only marginally different from the other two,
but which requires two source files like so:
CHAPTER 7 n ENTER THE ENTERPRISE: A DWR-BASED REPORT PORTAL 331
public interface MyInterface {
public void setMyBean(MyBean inBean);
}
public class MyClass implements MyInterface {
private MyBean bean;
public MyClass(MyBean inBean) {
this.bean = inBean;
}
}
This is clearly a little different, and you may wonder why you??™d want the extra code, and
the answer is the same answer to why use interfaces in the first place. If you have a class that
you want to have a known public API, and especially if you??™re going to have multiple implementations
of that class that must expose the same API and therefore can be treated as one
type (the type of the interface), then this is probably a desirable approach to DI.
At the end of the day though, this is all about getting dependencies into a class instance
without the class having to do the work itself, and Spring makes this very easy. We??™ll see the
details of doing this in Spring when we get to the application code, but you??™ll find it amounts
to nothing more than an XML configuration file and a new option in dwr.
Pages:
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589