This page last changed on Nov 03, 2005 by jdeolive.

Spring

Welcome to the home of the Spring Framework. As the leading full-stack Java/J2EE application framework, Spring delivers significant benefits for many projects, reducing development effort and costs while improving test coverage and quality.

Spring is the full meal deal for those of use who love to develop web applications. It can also easily be used in stand-alone (outside of the web envrionment) for client apps.

At the core of Spring development are Spring Beans, which are POJO's along with some XML describing them. Beans are the essentially the Spring plug-in mechanism. One of the nice things is that spring comes out of the box with a huge of beans for doing various things: JMX,JDBC,Web Services, etc...

Spring also offers the WebMVC framework. Those who are familiar with struts should feel right at home with WebMVC. It is the Spring Model-View-Controller framework for building views. And before you ask "Why not just use Struts?", I have found WebMVC to be less verbose and more intuitive then Struts. But for all you Struts lovers out there, Spring contains support for integrating with Struts.

Enough talk, lets SPRING into action (Sorry I had to).

Example

As stated before, everything in Spring is a bean. A bean consists of a POJO and some XML describing it. Lets create some beans for our Service example.

<beans>
  <bean id="example.service.wmsBean" class="example.service.wms.WMS"/>
  <bean id="example.service.wfsBean" class="example.service.wfs.WFS"/>
</beans>

That was easy, onto the client which will make use of the Spring WebMVC framework.

This document is not intended to be an introduction to WebMVC. It is recommened that you read this tutorial before continuing on.

The client will be a simple controller that performs the following task:

When a http request comes in, the url of the request is used to locate a particular service. If a service is located then the service outputs its capabilities document, otherwise an error is output.

Like everything else in Spring, the controller is a bean. The controller will need access to the wms and wfs (and any other service) beans. Therefore it must implement the org.springframework.context.ApplicationContextAware interface. This interface gives a bean access to its context, which in turn gives it access to other beans as shown below.

public class CapabilitiesServlet implements Controller, ApplicationContextAware {

	//application context used to access other beans
	ApplicationContext context;
	
	public ModelAndView handleRequest(
		HttpServletRequest request, HttpServletResponse response
	) throws Exception {
		
		//strip the service id from the url
		String req = request.getRequestURI();
		String serviceId = req.substring(req.lastIndexOf('/')+1);
		
		//use the context to find all installed services
		Map services = context.getBeansOfType(Service.class);
		for (Iterator i = services.values().iterator(); i.hasNext();) {
			Service service = (Service) i.next();
			//perform the id match
			if (serviceId.equals(service.getServiceId())) {
				//do it
				service.getCapabilities(response.getOutputStream());
				return null;
			}
		}
		
		//getting here means could not find the service
		response.getOutputStream()
			.write(("Could not locate service with id:"+ serviceId).getBytes());
		
		return null;
	}

	public void setApplicationContext(ApplicationContext context) 
		throws BeansException {
		
		this.context = context;
	}
}

Being a bean, we need to describe the controller with some xml.

<bean id="capabilitiesController" class="example.service.client.spring.CapabiltiesController"/>

We need to link our controller to a url. This acheived with the following bean declaration.

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/*">capabilitiesController</prop>
            </props>
        </property>
    </bean>

The final set of bean delcarations is placed in a file called 'example-servlet.xml'. It is called the application context and follows the Spring naming convention.

example-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="example.service.wmsBean" class="example.service.wms.WMS"/>
    <bean id="example.service.wfsBean" class="example.service.wfs.WFS"/>

	<bean id="capabilitiesController" 
		class="example.service.client.spring.CapabilitiesController"/>
	
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/*">capabilitiesController</prop>
            </props>
        </property>
    </bean>
</beans>

And the final step is to set up a dispatch servlet, that will do the above mapping. The servlet is declared in a standard web.xml web application descriptor.

web.xml
<web-app>
  <servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>example<servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

And that is it, we should be ready to roll. Download example.war and drop into your favorite servlet container. Try out the following:

http://localhost:8080/example/wms
http://localhost:8080/example/wfs
http://localhost:8080/example/none

This of course assumes your servlet container runs over port 8080.

Evaluation

Enough about the technology, lets apply our criteria.

Plug-in Architecture.

To be done...

Ease of Development

Spring IDE is a graphical user interface for the configuration files used by the Spring Framework. It's built as a set of plugins for the Eclipse platform.

Integration

This is one of the main pros of Spring. It claims to be powerful and complete, yet light and flexible enough to be used with or without other technologies and containers.

You can use all of Spring's functionality in any J2EE server, and most of it also in non-managed environments. A central focus of Spring is to allow for reusable business and data access objects that are not tied to specific J2EE services. Such objects can be reused across J2EE environments (web or across J2EE environments (web or EJB), standalone applications, test environments, etc without any hassle.

Spring's layered architecture gives you a lot of flexibility. All its functionality builds on lower levels. So you can e.g. use the JavaBeans configuration management without using the MVC framework or AOP support. But if you use the web MVC framework or AOP support, you'll find they build on the configuration framework, so you can apply your knowledge about it immediately.

Longevity

To be done...


full.gif (image/gif)
example.war (application/octet-stream)
Document generated by Confluence on May 14, 2014 23:00