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:
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.
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.
DriverManager
service.JdbcTemplate
.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.
The Spring Framework contains the following features:
Zoo
and Animal
, where Zoo
has an object of Animal
:new
operator. Here, the Zoo
class contains the object Animal
that we have instantiated using a new
operator, as shown in the 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
.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.
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.
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.
The following are the other features provided by the Spring Framework: