Category Archives: Spring Book

Spring Book – Part II – Core Support by Spring

Any enterprise application is typically made of multiple components working in tandem to realize a business case. Usually the objects communicate with each other in some form to achieve this in an application. Achieving component interaction can be cumbersome and difficult to implement in any application. Even if it is considered easy to do, it might become very hard to replace a component with other component due to challenging business scenarios. Spring Framework does this sort of component interaction which can be seamlessly changed with changing scenarios efficiently and easily.

Part 2 builds on what you learned in Part 1 by delving into greater detail on Spring and its various features. Part 2 also focuses on the main goals and objectives that the Spring Framework addresses in an enterprise application. It’s quite a big part which spans eight Chapters in whole and gives the various core supports which the Spring framework provides, so that you can achieve these capabilities with ease while developing your application.

In Chapter 3, “Spring Quick Start,” I will explain how Spring works and is the basic principles it is based on. We’ll take a detailed look at bean definitions, accessing bean instance and various bean scopes available in the Spring Framework. We’ll also dig into Dependency Injection, the ApplicationContext class, and the ApplicationContext Lifecycle. We’ll do a deep dive into the various phases of the lifecycle so that you can get a solid understanding of the crux of the Spring Framework.

In Chapter 4 “Configuration Styles”, I will be explaining the various configuration styles that you can use in your Spring applications. On the way I will be giving brief explanation on the various annotations that you could use and finally gives a gives idea on the best practices for using various configuration styles in your application.

In Chapter 5, “Application Configuration – Simplified”, I will be giving some ideas as to how you can use he various simplification methods whereby you can reduce the configuration in your Spring application. I will also give you best practices that you can follow in some details with code samples.

In Chapter 6, “Enterprise Integration”, I will be covering enterprise integration capability which the Spring framework provides, which helps to easily integrate your application components to other components within the same application or third party application.

In Chapter 7, “Testing”, I will first be covering the general testing methodologies in any application to some detail. After that I will be covering each testing methodology along with the support provided by the Spring framework.

In Chapter 8, “Data Access”,  I will be giving you details as to how Spring framework supports in interacting between data access layer and the service layer in your application.

In Chapter 9, “Transaction Management”, transaction support by Spring framework is explained in full detail along with various basic transaction concepts which is key in any enterprise application that you will develop.

In Chapter 10, “Caching”, I will be explaining the various caching mechanisms that you can use in your applications and Spring’s support to do this integration with ease.

Chapter 3: Spring Quick Start

Chapter 4: Configuration Styles

Chapter 5: Application Configuration – Simplified

Chapter 6: Enterprise Integration

Chapter 7: Testing

Chapter 8: Data Access

Chapter 9: Transaction Management

Chapter 10: Caching

Page Visitors: 619

Spring Book – Chapter 2 – Getting started with Spring

Developing enterprise application using Spring Framework is much simpler than using standard Java EE in many ways. Even though Java EE’s declarative programming model using EJB simplifies infrastructural aspects of development such as transactions and security, t still introduces unwanted complexity to the application as a whole. Developers need to still write more Framework code than the application code. Over the period Java EE has also redefined itself to introduce new programming techniques like Aspect Oriented Programming (AOP) and Dependency Injection (DI). So many new Frameworks got evolved using these techniques. Leading from this pack of Framework at the moment is by Spring Framework by a long way. In this chapter I will try to give you details on various aspects of Spring Framework, along with the sample application that will be used throughout this book to show the various concepts in this Framework.

In this chapter, I will give you details on how you can start building applications with Spring Framework. I will also give details on the sample application on which the entire book is based. It also talks about the some basic concepts which are very much essential for understanding Spring in a holistic way.

Developing with Spring Framework

Getting the Spring Framework

Before you get started, you need to get the Spring Framework. You have two options to do so: you can either download the package distribution from Spring website, or you can checkout the source from github.

Downloading standard package distribution

Visit http://www.springsource.org/download/community?project=Spring%2520Framework and download the latest Spring release (version 3.1.1.RELEASE at the time of writing). Previous releases can also be found in this same page.

Checking out source from github

Alternatively you can visit https://github.com/SpringSource/spring-Framework and can download as required or fork the Framework and create your own repository and play around with it.

Using Maven Build Framework

If you are using Maven Build Framework for building your application, you can download the necessary artifacts into your project by mere declaring the maven repository in your build xml called as pom.xml. You will have to make sure that your machine is appropriately configured so that your Maven can look into the repository URL and download the necessary artifacts which you require in your project. Declare the repository as follows in your pom.xml:

Listing 1-1. Repository declaration in pom.xml if you are using maven as your build tool

After that declare the spring modules that you require in your project as below (in the below example spring-context jar with version 3.1.1.RELEASE will be downloaded into the project and set dependency on):

Spring Packaging

The Spring Framework is a series of JAR files, which we can get by two ways as explained in the above section. To install it all you really have to do is extract the downloaded compressed file to some location on your computer, and then reference the appropriate JARs in a Java project.

Spring Dependencies Structure

Quite simply the dependency between various jars comprising the Spring Framework can be summarized as below:

Figure 1 Spring Framework Dependencies

Page Visitors: 7815

Spring Book – Chapter 1 – Introduction

Over the last few years, Spring has grown substantially, surpassing many other frameworks. Spring enables you to concentrate on writing less Spring-Framework-related code and more application code, which is vital in the fast-changing and cost-sensitive world of enterprise applications. Since its initial release in 2004, Spring has reinvented itself multiple times and become prominent in the industry as a strong force in the framework market. This has not only made the Spring Framework renowned, but it has also put healthy pressure or rather competition on well-known industry specifications (Java EE) and the application-server market. In the Java world, this is a very exciting time and a very important turning point.

In this chapter, I introduce Spring Framework as a whole (If I could, I would compel you to use it). Before explaining the nitty-gritty, I give you reasons to choose Spring for your application. Then I explain the Spring framework architecture and its features and, more important, introduce you to the main modules on which Spring Framework is based.

Why Spring?

Spring provides an extensive, portable, open source-application-development framework for Enterprise Java. Starting with Spring, you can build whatever you want: reservations system, banking systems, social networks, and more. Spring expands to meet your needs; you can integrate various security models, databases, web services, mobile clients, messaging systems, and so on that are required in today’s agile enterprise applications. Another key advantage is that Spring fits directly into your current infrastructure, because Spring uses the same Plain Old Java Objects (POJOs) you use every day. Once you start creating applications with Spring, you can deploy it everywhere in the certainty that it will thrive and adapt to the environment with ease.

If you need your application to be better, newer, and speedier, Spring is the right alternative for development. Surveys suggest that more and more Java developers around the world are moving toward Spring. For instance, recent analysis from Evans Data (“Spring Usage Study,” Evans Data Corporation, September 2011) analyzes the ubiquity achieved by Spring as summarized below:

  • Spring is the framework of choice among knowledgeable Java developers.
  • Spring developers report productivity and speed of deployment as the top reasons for using it: 70 percent of Spring users report at least a 50 percent productivity gain by using it, and 73 percent report that the framework allows them to deliver projects twice as fast.
  • It is estimated that more than two thirds of Java developers today are either using Spring or plan to do so within the next two years.
  • More than half of all existing Spring users expect to considerably increase their use of this framework over the next few years.
  • Spring users are more cloud ready and it is considered that heavy Spring adopters are more likely to have already deployed their applications to the cloud.
  • More than half of all existing Spring developers report that they plan to move their Java applications to a lightweight server, such as Apache Tomcat, within one year. ( Get more information from http://www.vmware.com/company/news/releases/vmw-spring-momentum-3-14-12.html )

Shared feedback from developers around the world highlights the following advantages of Spring Framework:

  • Easier to understand. Not rocket science.
  • If you have any issues with the framework code, you can just open the source code and with minimal effort understand it very easily.
  • Makes users highly productive, shortens development time.
  • For junior developers, it’s an excellent learning tool because so many live, proven Java pattern examples can be seen throughout the framework code.
  • Enables developers to cut out tons of redundant code, reducing boilerplate code in their applications. Programmatic catching of exceptions and cleaning up of resources, etc., are things of the past.
  • Spring’s ability to wire objects at runtime is incredible. Injection of right type-safe objects wherever needed is transparently handled by the Spring container.
  • A la carte framework—Plug-in what you want and use it.

What Is Spring Framework?

Spring Framework is built on well-defined goals and a coherent philosophy. Its main aim is to make Java EE development easier. This section explains Spring Framework’s main goals and its role in the development of enterprise applications.

Goal

Spring’s main aim, according to Rod Johnson (see sidebar) is to make Java EE easier to use and promote good programming practice. Even though POJO-based programming model is practiced in many enterprise applications, Spring Framework takes this concept into a different level by incorporating this model into it and making it applicable in a wide range of environments available. Due to this style of programming model, the framework keeps itself open to take advantages provided by the environment it works on without losing portability in the process.

Rather than Spring writing its own bit of code for common functionalities like logging, connection pools, transaction coordinators etc., it allows the developers to appropriately plug in the various packages available as open-source projects (For example, Commons Logging package can be used for all common logging purposes, Commons DBCP package can be used for connection pooling etc.) into the framework with mere configuration and with ease. By doing so it allows using the best package available in the open-source world and also keeps it open for future change as the case may be. All of this Spring does by keeping in mind that it doesn’t gets itself locked to any platform-specific or non-standard technology making it one of reason to use the framework in any enterprise application development.

Spring over the period has grown into a mature framework transforming itself into a one-stop shop capable of handling most of the infrastructural concerns which exists in a typical modern enterprise application. Applications are there because it has to support a business requirement. Spring provides applications with the capability of taking care of your core business objects easily and also allows changing it with minimal impact on the application as a whole according to changing business scenarios.

Rod Johnson is an enterprise Java architect specializing in scalable web applications. He has worked with both Java and JEE since their release and is also a core member of JSR 154 Expert Group entrusted with defining the Servlet 2.4 specification. He started developing frameworks for web application in 2000 while working as independent consultant. The Spring project was started by him in 2002, and it first official release was in March 2004. Johnson released this framework with the publication of his book Expert One-on-One Java EE Design and Development (Wrox Press, 2002).

Spring Framework’s Role in Enterprise Applications

Spring Framework includes core support for:

  • Application configuration
  • Enterprise integration
  • Data access
  • Testing
  • Web application-development support
  • Enterprise application-development support

Application Configuration

A typical application comprises various components working together to carry out a particular application-use case. Spring helps you build this configuration with ease. With Spring, components constituting a use case don’t worry about one another and at any point they can be swapped to keep up with the business change (agility).

Enterprise Integration

An enterprise application doesn’t work in isolation. It requires various enterprise services and resources such as security, messaging, caching, transactions, etc., to constitute an end-to-end application to meet real-life application needs. With ease and agility, Spring does this by keeping your application code simple and both unit and integration testable. It can be easily plugged into any standard Java EE application server and also is capable of stand-alone usage.

Data Access

An enterprise application accesses data stored in a relational database to carry out various business functions and to enforce various business validations. Spring makes data access easy and effective. It supports all major data-access technologies, such as Hibernate, JPA, JDBC, etc.; manages these resources transparently from the developer; and also offers tons of APIs, which enables developers to concentrate more on the core business coding and thereby increase productivity.

Testing

Unit and integration testing is essential for any enterprise application. It helps with decompiling objects from the environment where it resides and makes it easier to test the application in isolation from its actual deployment environment. More important, it helps in system/integration testing to see that all the components constituting an enterprise application work properly. This testing can be done in the actual environment as well as in isolation from the actual environment, which is a big plus compared to other framework platforms.

Web Application-Development Support

Spring includes a powerful and highly configurable Model-View-Controller web framework called Spring MVC. Even though very much similar to Apache Struts, it can never be said as derived from it. Frankly saying no copyright infringement spotted.  Now to look into the similarities, Struts Action class is similar to Spring MVC Controller class and this object is multi-threaded in nature will a single instance being used for servicing all the clients.  Spring MVC has some significant advantages over Struts, however—it has a powerful navigation rule framework, Spring Web Flow, which equals its powerful web framework for application development. Even though Spring has its own implementation for a web framework, it has kept its main goal of seamlessly integrating with other famous web frameworks, such as Struts and Java Server Faces (JSF).

Enterprise Application-Development Support

Spring’s core strength lies in its ability to integrate with common and most required services in enterprise applications. It has modules for creating and maintaining web services, scheduling jobs, integrating with messaging services, and so on. This modular coding support ensures good manageability for applications.

Page Visitors: 2300

Spring Book – Part 1 – Overview

Spring Framework is a Java platform that provides all-inclusive infrastructure support for developing JEE applications. It handles the almost all the plumbing/boilerplate code required for your application so that you can focus on your business specific logic while building your enterprise application. Simple Plain Old Java Objects (POJOs) becomes powerful objects having middleware services like security, transaction management etc. inside the Spring Container. Part 1 introduces you to the Spring Framework and also tries to introduce various core concepts on which Spring Framework is built on.

In Chapter 1, “Introduction”, you will get to know overview, history, overall architecture, building blocks, and features of the spring framework in detail.

In Chapter 2, “Getting Started with Spring Framework”, I will show you how to get set up for building applications with Spring Framework, and I will introduce the sample application on which the book is based. Chapter 2 also introduces some basic concepts that are essential for understanding Spring in a holistic way.

So here we go, I hope you will enjoy reading this book as much as I did in writing it for you.

Chapter 1: Introduction

Chapter 2: Getting Started With Spring

Page Visitors: 778