Java Reference
In-Depth Information
AttachmentDao , ContactDao , and so on. You would then configure which
implementations you'd like to use in a dependency injection frame-
work. The framework takes care of “wiring up” classes and their depen-
dencies for you. It becomes much easier to use different implementa-
tions, because you need to change only the configuration. Moreover,
you can use different configurations for different situations, making
testing much easier, as we'll see in the next section. All of this leaves
your code much more flexible because you have only references to
interfaces. The trade-off is more complexity; you no longer see which
implementations are being used when you're reading the code. You
have to look at the configuration of the dependency injection frame-
work to figure it out.
We'll look at the support that Stripes provides for dependency injec-
tion (DI) with Spring ( http://www.springframework.org ) . Guice ( http://code.
google.com/p/google-guice ) is another popular choice; since Stripes
does not have built-in support for Guice, we'll take the opportunity
to implement it ourselves as an exercise in Section 13.4 , Interceptor
Example: Adding Support for Guice, on page 299 .
Setting Up Spring
Because Spring provides many other services besides dependency in-
jection, the distribution comes with several JAR files so that you can
use only what you need. In our case, we need spring-core.jar , spring-
beans.jar , spring-context.jar , and spring-web.jar to use the DI container and
the web application context loader. You can also take the easy way out
and just use the spring.jar file, which includes everything.
Next, set up the Spring context loader listener in web.xml :
Download email_25/web/WEB-INF/web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Spring's ContextLoaderListener automatically loads the default Spring
configuration file, WEB-INF/applicationContext.xml , 4
when the web appli-
cation starts up.
4. You can also use different configuration files by indicating them in the contextCon-
figLocation context parameter.
 
Search WWH ::




Custom Search