Chapter 1. Introducing the Spring Framework

In this chapter, we'll introduce you to the Spring Framework. We'll also summarize some of the other features of Spring. We'll then discuss the Spring Architecture as well as the benefits of the Spring Framework. We will create your first application in Spring and will look into understanding the packaging structure of the Spring Framework. This chapter serves as a road map to the rest of this book.

The following topics will be covered in this chapter:

  • Introducing Spring
  • Spring Framework Architecture
  • Benefits of the Spring Framework
  • Creating a first application in Spring

Spring is an open source framework, which was created by Rod Johnson. He addressed the complexity of enterprise application development and described a simpler, alternative approach in his book Expert One-on-One J2EE Design and Development, Wrox.

Spring is now a long-time de-facto standard for Java enterprise software development. The framework was designed with developer productivity in mind, and it makes it easier to work with the existing Java and Java EE APIs. Using Spring, we can develop standalone applications, desktop applications, two-tier applications, web applications, distributed applications, enterprise applications, and so on.

As the title implies, we introduce you to the Spring Framework and then explore Spring's core modules. Upon finishing this chapter, you will be able to build a sample Java application using Spring. If you are already familiar with the Spring Framework, then you might want to skip this chapter and proceed straight to Chapter 2, Inversion of Control in Spring.

Introducing Spring

Spring is a lightweight Inversion of Control (IoC) and aspect-oriented container framework. Historically, it was created to alleviate the complexity of the then J2EE standard, often giving an alternative model. Any Java EE application can benefit from the Spring Framework in terms of simplicity, loose coupling, and testability.

It remains popular due to its simple approach to building applications. It also offers a consistent programming model for different kinds of technologies, be they for data access or messaging infrastructure. The framework allows developers to target discrete problems and build solutions specifically for them.

The Spring Framework provides comprehensive infrastructure support for developing Java EE applications, where the Spring Framework handles the infrastructure and so developers can focus on application development.

Considering a scenario of JDBC application without using the Spring Framework, we have a lot of boilerplate code that needs to be written over and over again to accomplish common tasks. Whereas in Spring JDBC application, which internally uses plain JDBC, the JdbcTemplate class eliminates boilerplate code and allows the programmer to just concentrate on application-specific logics development.

  • For a plain JDBC application without Spring, follow these steps:
    1. Register driver with the DriverManager service.
    2. Establish a connection with the database.
    3. Create a statement object.
    4. Prepare and execute an SQL query.
    5. Gather and process the result.
    6. Perform exception handling.
    7. Perform transaction management.
    8. Close JDBC object.
  • For a Spring JDBC application (internally uses plain JDBC), follow these steps:
    1. Get access to JdbcTemplate.
    2. Prepare and execute an SQL query.
    3. Gather and process the result.

Spring's main aim is to promote good programming practice such as coding to interfaces and make Java EE easier to use. It does this by enabling a Plain Old Java Object (POJO)-based programming model, which can be applicable in a wide range of development environments.

Technically, a POJO is any ordinary object that should not implement pre-specified interface or extend pre-specified class or contains annotation.

The following is the code for the POJOClass.java class:

package com.packt.spring.chapter1;

/* This is a simple Java Class – POJO */
public class POJOClass {

    private String message;
 
    public String getMessage() {
         return this.message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}

In the preceding code snippet, we have POJOClass containing a field and corresponding getter and setter methods. This class is a POJO class as it is not extending or implementing any class or predefined interface of Spring API.

Spring is modular, allowing you to use only those parts that you need, without having to bring in extra complexity. The Spring Framework can be used either for all layer implementations or for the development of particular layer of an application.

Features of Spring

The Spring Framework contains the following features:

  • Lightweight: Spring is described as a lightweight framework when it comes to size and transparency. A lightweight framework helps in reducing complexity in application code. It also helps in avoiding unnecessary complexity in its own functioning. A lightweight framework won't have a high startup time and will run in any environment. A lightweight framework also won't involve huge binary dependencies.
  • Non-intrusive: This means that your domain logic code has no dependencies on the framework itself. The Spring Framework is designed to be non-intrusive. The object in a Spring-enabled application typically has no dependencies on any predefined interface or class given by Spring API. Thus, Spring can configure application objects that don't import Spring APIs.
  • Inversion of Control (IoC): Spring's container is a lightweight container that contains Spring beans and manages their life cycle. The core container of the Spring Framework provides an implementation for IoC supporting injection. IoC is an architectural pattern that describes the Dependency Injection needs to be done by external entity rather than creating the dependencies by the component itself. Objects are passively given their dependencies rather than creating dependent objects for themselves. Here, you describe which components need which service, and you don't directly connect your services and components together in your code. Let's consider an example: we have two classes Zoo and Animal, where Zoo has an object of Animal:
    • Without Dependency Injection: This is a common way to instantiate an object is with a new operator. Here, the Zoo class contains the object Animal that we have instantiated using a new operator, as shown in the following screenshot:
      Features of Spring
    • With Dependency Injection: Here, we supply the job of instantiating to a third party, as shown in following screenshot. Zoo needs the object of Animal to operate, but it outsources instantiation job to some third party that decides the moment of instantiation and the type to use in order to create the instance. This process of outsourcing instantiation is called dependency injection.
    Features of Spring

    The Spring Framework promotes loose coupling by using the technique known as IoC. We'll talk more about IoC in Chapter 2, Inversion of Control in Spring.

  • Aspect-oriented Programming (AOP): This refers to the programming paradigm that isolates supporting functions from the main program's business logic. It allows a developer to build the core functionality of a system without being aware of additional requirements.

    AOP is used in the Spring Framework to provide declarative aspects such as transactions and security. Here, application objects perform business logic and are not responsible for other system concerns such as logging, security, auditing, locking, and event handling. AOP is a method of applying middleware services such as security service, and transaction management service on Spring's application.

    Let's consider a payroll management application where there will be Employee Service, HR Service, and Payroll Service, as shown in the following figure, which will perform some functional requirement to the system such as add/update employee details, remove employee, browse employee details, and much more. While implementing business functionality, this type of application would also require nonfunctional capabilities such as role-based access and logging details. AOP leaves an application component to focus on business functionality. Here, the core application implements the business functionality and is covered with layers of functionality provided by AOP for security, logging, and transaction management.

    Features of Spring

    Aspects can be added or removed as needed without changing your code. Spring aspects can be configured using its own IoC container. Spring AOP includes advisors that contain advice and pointcut filtering.

  • JDBC exception handling: The JDBC abstraction layer of the Spring Framework provides an exception hierarchy. It shortens the error handling strategy in JDBC. This is one of the areas where Spring really helps in reducing the amount of boilerplate code we need to write in the exception handling. We'll talk more on Spring JDBC in Chapter 3, DAO and JDBC in Spring.
  • Spring MVC Framework: This helps in building robust and maintainable web applications. It uses IoC that provides separation of controller logic. Spring MVC Framework, which is a part of the Spring Framework licensed under the term of Apache license, is an open source web application framework. Spring MVC Framework offers utility classes to handle some of the most common tasks in web application development.
  • Spring Security: This provides a declarative security mechanism for Spring-based applications, which is a critical aspect of many applications. We'll add Spring Security to our web applications in Chapter 5, Spring Security.

Other features of Spring

The following are the other features provided by the Spring Framework:

  • Spring Web Services: This provides a contract-first web services model, whereby service implementations are written to satisfy the service contract. For more information, check out http://static.springsource.org/spring-ws/sites/2.0.
  • Spring Batch: This is useful when it's necessary to perform bulk operations on data. For more information, refer to http://static.springsource.org/spring-batch.
  • Spring Social: Social networking, nowadays, is a rising trend on the Internet, and more and more applications such as Facebook and Twitter are being outfitted with integration into social-networking sites. To know more, have a look at http://www.springsource.org/spring-social.
  • Spring Mobile: Mobile applications are another significant area of software development. Spring Mobile supports development of mobile web applications. More information about Spring Mobile can be found at http://www.springsource.org/spring-mobile.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.91.37