Integrating with Spring: Core Concepts

In Servlet-based web applications, the ServletContext is the place to store application-relevant data. Each Servlet-based application has exactly one Servlet context object where you can store, for example, configuration data. The Servlet context can be accessed by any of the Servlets declared in your application by means of many of the javax.servlet specific types such as ServletRequest, ServletResponse, ServletConfig, and FilterConfig. When developing a web application, the ServletContext is also the place where Spring stores the application context(s) for your application (have a look at Figure 14-1).

image from book
Figure 14-1

The examples shown in this chapter all use the org.springframework.prospring.ch14.OrderService interface, declaring exactly one method:

public Order getOrder(String orderId);

In our application, we've implemented the OrderService and defined it in an application context (located in /WEB-INF/applicationContext.xml) as follows. Note that for this example we've chosen to leave out any dependencies on data-access objects and such.

<bean     /> 

You might remember from earlier chapters that loading an application context in a web application can be done using the ContextLoaderListener (or in non–Servlet 2.4–compliant environments using the ContextLoaderServlet). We'll load the application context with our order service as follows. Note that you can tweak the location of the application context by setting the contextConfigLocation at context parameter level. This way you can include more than one context or override the default name (/WEB-INF/applicationContext.xml).

<web-app>       <listener>     <listener-class>       org.springframework.web.context.ContextLoaderListener     </listener-class>   </listener>       ... </web-app>

To show you how to retrieve an ApplicationContext from the ServletContext, we'll develop a small Servlet:

public class ShowOrderServlet extends HttpServlet {   private OrderService orderService;       public void init() throws ServletException {         ServletContext sContext = getServletContext();     ApplicationContext appContext =       WebApplicationContextUtils.getRequiredWebApplicationContext(sContext);         orderService = (OrderService)appContext.getBean("orderService");   }       protected void doGet(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException {         String id = RequestUtils.getParameter(req, "orderId");      Order order = orderService.getOrder(id);   } }

The WebApplicationContextUtils class is part of Spring and resides in the org.springframework. web.util package. Using this class you can retrieve the application context stored in the ServletContext from anywhere in a web application, provided that you have the ServletContext available. There are two variants of retrieving the application context. One is to just call getWebApplicationContext(), which will return null if no context was found. The getRequiredWebApplicationContext() method throws a java.lang.IllegalStateException if no context could be found. The latter is perfectly suited for retrieving an application context during an initialization phase and when the context is absolutely required.

Of course this is not the way you would like to develop web applications in general (developing a Servlet per action) and certainly not the way you would like to retrieve your dependencies. The following sections describe how to retrieve (or rather inject) dependencies in your Struts or WebWork actions using more advanced mechanisms.

One interesting thing still left to mention here is the auto wiring capabilities of Spring's application contexts. We'll look into using the auto wiring features in more detail while discussing the Struts integration, but after you've looked up a Spring WebApplicationContext, one of the things you can do is auto wire the object from which you have looked up the context. Although the object isn't wired in a Spring context, this way it can still benefit from Spring's IoC features. Use the following line of code to auto wire your object by type, where this could of course be replaced by any arbitrary object. Dependencies expressed in the form of setters will now automatically be satisfied, based on the argument type of the setter.

 context.getBeanFactory().autowireBeanProperties(this,   AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); 

Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188 © 2008-2017.
If you may any questions please contact us: