Category Archives: Spring Book

Spring Book – Chapter 16 – Web Services – Spring WS

In June 2000, Microsoft coined the term “Web Services” when they introduced this as one of the key component of its .NET framework. As other industry players started adopting this, it was evident that this technology would revolutionize distributed computing in the coming years. In the past few years many techniques have been developed to help applications interact with each other, of which prominent one is Web Services by a large extend.

Spring Web Services (Spring WS) follows the strategy of contract-first web services. It focuses more on XML and lesser on Java implementation. The underlying details are completely under the control of developers starting from the contract to the marshalling/unmarshalling details to the endpoint which actually handles the request.

Spring WS being a product of the Spring Framework, offers first class support of using Spring configurations and integrating with Spring framework. It provides ability to plug-in various XML APIs (SAX, DOM, StAX, XOM, and JDOM) to handle XML messages and can always ensure that the XML contract is marshaled correctly into Java objects. It supports JAXB 1 and 2, Castor, XMLBeans, JiBX, and XStream as part of its XML marshalling support.

When you have finished with this Chapter, you will have clear idea of all the terminologies related to web services and will also have a clear idea of the support provided by Spring to create and use web services in your application.

What is a Web Service

A Web service is a method of communication between two electronic devices over the Web (Internet).

– wikipedia.org

A Web service is a unit of managed code that can be remotely invoked using HTTP protocol which allows exposing the functionality of your existing code over the network which other application can use it.

To give you a better explanation of what a web service is, I would like to give you the exact working of web service pictorially shown in Figure 16-1 below.

fig16-01

Figure 16-1. Web service architecture

The various steps involved in actually consuming an already exposed web service can be summarized as below:

  1. The client (web service consumer), queries the UDDI registry to get the details of the service and to locate it in the network (Internet).
  2. The UDDI registry refers the web service requesting client to the WSDL document details.
  3. The requesting client accesses the WSDL document.
  4. The WSDL document gives all the relevant details of the requested web service to the client.
  5. The client now sends SOAP-message request to the actual web server exposing the service.
  6. The web service returns the SOAP-message response which the client receives and does the appropriate processing.

Concepts

Spring hides many of the web service complexities away from the developer. But since some concepts are very crucial in overall understanding of web services, we’ll take a brief look at some these concepts in the following sections.

SOAP Messages

SOAP (Simple Object Access Protocol) is an XML vocabulary that lets programs on separate computers interact across a network via RPC (Remote Procedure Call). A SOAP message can be of three types summarized as below:

  • Method call – it contains name of method and parameters.
  • Method Response -contains the return values.
  • Fault Message – if service throws exception, it produces SOAP fault message. Due to any reason if transport fails, it will give appropriate standard HTTP messages

The SOAP message structure is illustrated in Figure 16-2.

 Figure 16-2. A typical SOAP message structure

Figure 16-2. A typical SOAP message structure

Page Visitors: 11134

Spring Book – Part IV – Enterprise Application Development

Enterprise application requires many services to realize various uses cases. These services are mandatory and form the backbone of the application in many ways. Changing business scenarios requires changing and updating these services at various changes according to demand. With Spring’s capability, this can be achieved very easily catering integration with evolving opens-source technologies and solutions.

This part of the book contains Chapters which will help you in developing enterprise application having all the required enterprise services helping you realize business use case with the help of Spring Framework.

Chapter 16, “Web Services – Spring WS”, introduces you to the Spring’s capability of supporting web services in your application. After reading through this Chapter, you will have clear idea of all the terminologies related to web services and will also have a clear idea of the support provided by Spring to create and use web services in your application.

Chapter 17, “Messaging with Spring”, will initially cover messaging concepts in some detail. Later on we will cover JMS and its components in detail. We will then deep-dive into the support provided by Spring in applying messaging to your application. We will then go through the transaction support by JMS and again what Spring has to offer in this space. Last but not the least, the Chapter concluded with introduction and Spring’s support in achieving global transactions in your application.

Chapter 18, “Remoting”, we will first go through the basic concepts in remoting and then go on to the support provided by Spring. We will then go on in detail to cover the various remoting technologies which Spring supports in detail.

Chapter 19, “Tasks and Scheduling”, initially looks into the various concepts dealing with concurrency and then looks into Java support for doing this. Later on it looks into how Spring Framework can support doing this in your application. You should also be able to configure and use Quartz in your Spring application as well have the capability of using JDK’s timer support in your application.

Chapter 20, “Spring Integration”, will help you start with Spring Integration and will aid you in achieving the various integration requirement in your application. I am sure after reading through this Chapter you will have one more weapon up your sleeves which you can use and reap benefits from its high level of integration capabilities.

Chapter 21, “Spring Batch”, similar to other chapters will introduce you to Spring Batch in a comprehensive manner. Batch operation is one among the integral parts of modern day application and Spring’s support comes in the form of Spring Batch and we will deep dive into this in all aspects possible.

Chapter 16: Web Services – Spring WS

Chapter 17: Messaging with Spring

Chapter 18: Remoting

Chapter 19: Tasks and Scheduling

Chapter 20: Spring Integration

Chapter 21: Spring Batch

Page Visitors: 647

Spring Book – Chapter 13 – Spring and JSF Integration

JSF being one of the very successful, prominent and well backed up web framework, the importance of integration between Spring and JSF is covered in full detail with meaningful pictures and example code in this chapter for readers who are beginners and experts alike.

JavaServer Faces (JSF) is a server side user interface component framework for developing Java technology-based web applications. Developed through the Java Community Process under JSR – 314, it establishes the standard for building server-side user interfaces and have gained tremendous acceptance over the period of time in the Java world. JavaServer Faces APIs are being designed keeping in mind that they can be leveraged by tools that will make web application development even easier and simpler.

This Chapter provides an introduction on how the in famous Spring Framework can be integrated with in famous web framework, Java Server Faces Technologies. The Chapter covers the necessary details of both the Spring Framework and JSF technology in the initial section in the context of Integration and then later on moves towards the concept of concepts which help in easing the integration between the two technologies.

JSF Overview

Before getting into the details of how Sping and JSF Integration, it is important to know basic details of JSF. The following initial sections of this Chapter will cover JSF in some detail so that initial context is set and we can understand the integration in a clear fashion.

JSF Architecture

JSF is based on Model-View-Controller (MVC) architecture. Since it is based on well know MVC design the applications can be designed in a simpler and easy fashion keeping maintainability factor intact. In earlier Chapters we have covered MVC design patter in detail, so I wouldn’t repeat the same in this Chapter.

MVC model’s main purpose is to separate model and presentation to enable developers to set focus on their core skills and collaborate more clearly. If the application demands creating different pages for presentation, developers can concentrate on the view layer rather than model and controller layer as developers have a provision to reuse code for controller and model already written with ease.

Pictorially JSF architecture keeping the essence of MVC design can be represented as shown in Figure 13-1 below.

fig13-01

Figure 13-1. JSF architecture showing off MVC design

JSF Lifecycle

The processing of a request is called lifecycle in JSF. There are different phases which are executed one after the other following “Chain of responsibilities” design pattern. Figure 13-2 below shows the JSF lifecycle.

fig13-02

Figure 13-2. JSF lifecycle

There are various phases constituting the JSF lifecycle. The various lifecycle phases can be summarized as follows:

  • Restore View
  • Apply Request Values
  • Process Validations
  • Update Model Values
  • Invoke Application
  • Render Response

Pictorially the various phases in the JSF lifecycle can be as shown in Figure 13-3 below.

fig13-03

Figure 13-3. JSf lifecycle phases

JSF application in many ways work very much similar to a typical desktop application comprising of components and events. In line with client application, components can create events and can react or listen to events. There are different types of events and there are various places where these events are created and invoked. The events can be classified into three different types as shown below:

  • System events – These events are created at various stages in an application or request lifetime. These events can be configured to evoke globally to all components and others which are related to a component.
  • Phase events – Phase events are coarse grained in nature and are broadcasted before and after every phase. You can use phase listener to check access before a phase is entered. You also have a provision of registering global phase event listener and per view phase event listener. Global phase listener are executed for all requests whereas per view listener only for a request of the specific view.
  • Application events – Application events are created by the application components. JSF has two types of events namely:
    • ValueChangeEvent
    • ActionEvent

Figure 13-4 below pictorially shows the events which are invoked at the various phases in the JSF lifecycle.

fig13-04

Figure 13-4. JSF lifecycle phases along with event invocation points

Page Visitors: 3882

Model View Controller 2 architecture

Spring Book – Chapter 12 – Spring and Struts Integration

My humble effort to explain Spring and Struts integration in detail using code samples for easy understanding for my fellow developers and readers.

Apache Struts is more established, more used and well known MVC2 web framework in the Java world. I am sure anyone experienced with web application will surely have had a chance to work with this one of the pioneers in web framework.

Over the past few years there have been so many buzz words in the world of software development like Inversion of Control (IoC), AOP etc. These paradigms has revolutionized the software development and Spring being one of the frameworks adopting these new technologies into it and have become really good at it.

This Chapter makes a big assumption that the reader is conversant with the Struts Framework and that the reader would like to integrate the Spring Framework into the struts application so as to use the capabilities and out-of-box features provided by the Spring Framework.

If you like to take advantage of these features of Spring you do not have to rebuild the application, but you can integrate your existing Struts application with Spring without much hassle. This Chapter will let you do exactly this so that you can take the advantages of both worlds with good level of confidence and comfort. This Chapter is a very simple one and I am sure you will be able to complete it at one go without trouble whatsoever.

Apache Struts

Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.

Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.

Apache Struts in an MVC2 Java web framework. MVC2 is a servlet-centric architecture.  MVC2 incorporates a clear separation of view and controller. A controller receives all the requests, retrieves data from a Model and forwards it to next view for presentation.

In MVC2, there is a central Servlet (Controller) which calls business logic (Model) and the forwards it particular JSP page (View) that is why it is also called servlet-centric architecture. Pictorially MVC2 implementation is as shown in Figure 12-1 below.

 Model View Controller 2 architecture

Figure 12-1. MVC2 (Servlet-centric architecture)

WebWork was a Java-based web application framework developed by OpenSymphony which got merged into the current Struts2 web application framework. WebWork is built on top of XWork, which provides a generic command pattern framework as well as an Inversion of Control container.

Advantages of integrating Struts with Spring

The advantages of integrating a Struts application into the Spring Framework are:

  • Spring framework is based on new design approach and was designed to resolve the existing problems of existing Java applications. To a larger extent Spring has been successful in living to the expectations. Integrating Struts application with Spring buys in all the advantages provided out-of-box by Spring.
  • Spring framework lets you apply AOP (aspect-oriented programming technique) rather than object-oriented code. This is quite useful in addressing some very core concerns in any modern day enterprise application.
  • Spring provides more control on struts actions. That may depend on the method of integration you choose which we will cover in detail in the following sections of this Chapter.

Struts History

Before going through Spring and Struts integration in detail, because of the evolution complexity of Struts, we would have to first go through the Struts history with core focus on the versions. According to the Struts version that you use in your application, the integration methodology changes accordingly.

Struts were originally developed by the programmer and author Craig R. McClanahan and were later taken over by the Apache Software Foundation in 2002. Strut1 with all standard Java technologies and packages of Jakarta assists to create an extensible development environment. However, with the growing demand of web application, Struts 1 does not stand firm and needs to be changed with demand. This leads to the creation of Struts 2, which is more developer friendly with features like Ajax, rapid development and extensibility.

The team of Apache Struts and another J2EE framework, WebWork from OpenSymphony joined hand together to develop an advanced framework with all possible developing features that will make it developer and user friendly.

Even though the new version is Spring 2, we would still cover integration of Spring with Struts 1 as well so that if existing Struts 1 project needs the various capabilities of Spring, they can do so without much hassle.

Page Visitors: 6423

Spring Book – Chapter 11 – Spring Web

Large number of enterprise web applications is developed using Java and forms a very integral part of any organization in modern day. Web applications have become more and more complex and it poses more and more challenges day by day. Having a good web framework should be able to address most of these challenges there by reducing the effort on the developer and allowing them to focus on implementation of actual business logic.

This Chapter introduces you to the Spring Frameworks web support and the various components in the Spring web stack. This is just an introduction to the Spring’s frameworks role in the web application development support.

The Chapter starts off by explaining the Spring web stack and the various components which it comprises. Later on it delves deep into each component and explains each one in detail. Following with the Spring philosophy it then covers the various integration possible with your Spring application. Since some integration is really important, these are covered as separate chapters later on.

A Little History

Before going deep into the Spring web stack, a little history about the Spring web stack development from its early days to present.

Spring started its journey in 2002 and Spring Web was part of the original Spring ecosystem and contained servlet/web tier tool for aiding web application development. Then came Spring Frameworks own MVC framework in the form of Spring MVC. Following the core principles of Spring, it allowed to integrate with other frameworks.

Then came the inclusion of Spring Portlet MVC which was contributed initially by Rutgers in April 2004. John Lewis started contributing changes and it got included in the main Spring code base along with Spring 2.0 release.

Spring Web Flow started as a sub project and its first release was in March 2005. Due to its innovative ideas and good acceptability in the industry after inception, it soon became an independent project and got its independent release version 1.0 released in the same year.

Over the period Spring MVC and Spring Web Flow evolved and is considered among the renounced web technology stack.

Spring Web Stack

Modern day web application posses high level of challenges, including the following:

  • Complexity continues to rise at an alarming rate
  • Necessity of exposing services to a larger audience
  • Ability to give users the best possible experience in the shortest possible of time
  • High expectations
  • Ability to build simpler applications
  • Cloud enablement

Ideally any web framework chosen for web application development should address the above challenges reducing much of the complexity for the developer. Spring provides a full stack web framework by gathering multiple libraries and frameworks useful for web development into a single unified software stack for web developers to use. It tries to address the rising complexities raised by web application development by building on top of the lightweight Spring container. Figure 11-1 shows the various components provided by the Spring web stack for developing web application using this foundation.

Figure 11-1. Spring Web Stack components

The Spring Web Stack gives you:

  • A unified programming model
  • Capability to handle multiple client types using the same foundation
  • Adaptability to choose the right approach according to the required use case

The various Spring web stack components are:

  • Spring Framework and Spring MVC
  • Spring Web Flow
  • Spring Security
  • Spring JavaScript
  • Spring Faces
  • Spring BlazeDS Integration

Page Visitors: 2700

Spring Book – Part III – Web Application Development Support by Spring

For any organization, business applications based on web has become a sort of mandate due to its evolvement over the period of time. Spring Framework provides modules which aids in developing web application infrastructure for the server side.

This part of the book contains Chapters which will help you in developing web application with the help of Spring Framework. More importantly it lets the reader understand the web application development support by the Spring Framework.

Chapter 11, “Spring Web”, introduces you to the Spring web stack as a whole. It introduces you to various components in the Spring web stack and also covers some basics around Spring web and other web technology facts like MVC framework and so forth.

Chapter 12, “Spring and Struts Integration”, purely looks into the integration of Spring Framework with the famous MVC framework, Struts in detail.

Chapter 13, “Spring and JSF Integration”, similar to Chapter 12, looks at integrating Spring Framework with another famous MVC framework Java Server Faces (JSF) in detail.

Chapter 14, “Spring MVC and Spring Web Flow”, goes into detail of Spring MVC, Spring Frameworks implementation of MVC pattern and Spring Web Flow in detail.

Chapter 15, “Web Application Security with Spring”, covers the most important aspect of any enterprise application, security. It covers Spring Security in detail with respect to web application security.

Chapter 11: Spring Web

Chapter 12: Spring and Struts Integration

Chapter 13: Spring and JSF Integration

Chapter 14: Spring MVC and Spring Web Flow

Chapter 15: Web Application Security with Spring

Page Visitors: 335

Spring Book – Chapter 10 – Caching

Cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the greater the number of requests that can be served from the cache, the faster the overall system performance becomes.

–          wikipedia.org

“Cache” is a specialized form of computer memory.

–          About.com

A cache stores recently-used information in a place where it can be accessed extremely fast.

–          techterms.com

 To store data locally in order to speed up subsequent retrievals.

–          pcmag.com

One of the new features added in Spring Framework in its version 3.1 is its support for transparently adding caching into an existing Spring application. Similar to transaction management, it allows developers to incorporate consistent use of various caching solution available in market with minimal or no impact on the source code.

In this chapter we will first go through various caching concepts and why it should be used in your application. We will then delve deep into as to how we can do this in your Spring application. In these sections we will cover the various nitty-gritties involved in configuring caching for your application. We will also see Spring’s support for other third party cache libraries available today and then finally wrap the chapter with the concept of distributed caches to some extent.

After reading this chapter you should have a clear idea of the cache abstraction provided by the Spring framework and also have a very clear idea of using caching in your existing and new application build on Spring Framework.

Why Caching?

Caching has been one of the methods employed to speed up an application and relieve its workload. Its usefulness is particularly apparent today with the rise of social web applications, visited by thousands of people at the same time. Moreover caching should be something orthogonal, having minimal or nil side-effects to the business logic that a developer writes. There are many cache libraries in Java world, both commercial and open source.

Caching is an essential tool for scaling web applications. It avoid disk access, I/O, CPU power and network traffic – primarily to the backend relational database in modern web applications.


Application without Caching Layer

An application that doesn’t have a caching layer has the following  known problems:

  • Every call creates new objects in CPU memory and Java heap.
  • Duplicate objects in Java heap due to repeated database calls.
  • All data comes from database which involves network call and is very expensive in nature.
  • Due to network involvement in various steps in the overall execution, response times get affected.
  • A single call might in turn call multiple data sources to get all required data, in which case it becomes more expensive and more time consuming.
  • Due to high amount of objects in Java heap, it could trigger Java garbage collection which is very expensive.
  • There is an additional requirement of CPU in doing the marshalling of data from database to the format required in the application layer.

Figure 10-1 shows a typical application without caching layer. It shows applications interacting with more than one data source to retrieve all the required data in your application. It also shows interaction of application directly with various data sources which are very expensive and also time intensive.

Figure 10-1. Application interactions without caching layer

Page Visitors: 13486