The main content of the second day is some detail about the Spring Framework,
including elaboration of the core concepts in Spring, how to set up the
framework, and XML file based configuration as well as annotation based
configuration. Apart from the core framework, Spring MVC is also mentioned.
Spring IoC Startup
The core module of the Spring project is the Spring Framework which provides
the functionalities to manage objects as well as their dependency relationship
in large application. Spring implemented an object container that can be configured via
either XML file or annotation (there is also a code based configuration).
Concepts
Spring implemented a container to manage the lifecycle of Java object, it also introduced
or involved a set of concepts which are different from the manually managed objects.
IoC: Inversion Of Control;
DI: Dependency Injection;
BeanFactory: basic container to manage lifecycle of managed objects. It providing basic support for DI;
ApplicationContext: more advanced container to manage lifecycle of managed objects.
It provides more features including listener registration for handling certain change of application context;
Set up Spring Framework IoC
Spring Framework is released and distributed in a serial of jar (Java ARchive) file. These file can be acquired
from its official site: spring.io. With the assist of IDE, the jar files can be downloaded
automatically. When all of the jar files are accessible in classpath, then the Spring IoC container is
ready to use.
The following code is a simple sample shows the IoC container of spring.
1
2
3
4
5
6
// Interface class: convention between class consumer and provider
Dependency injection is used to decouple the dependency of an class consumer with the implementation of the class.
The most simple approach to use a class is instantiate an object while this process is generally highly related
to the implementation of the class. When the class provider and the class consumer make a conversion (a Java interface),
the class consumer can use the class without knowledge about its implementation. This class should be instantiate in advance
and inject into the consumer class to use it. This is DI.
Class Instantiation
To create an instance of dependency class, the most basic usage is create via default or no-argument constructor. The example
of this process is show in previous section.
Class Instantiation with Argument
It’s common that the constructor of a class has argument. This can be specified in XML.
The following code shows the XML configuration with constructor arguments.
Most dependency classes have their own dependencies as well. Their dependency can be
injected by construction argument or setter method (next section).
The following code shows the constructor based DI.
To have more control to the field of class, most class filed are implemented as private field
and access and modify them via a pair of methods named getter and setter.
The following code shows the setter based DI.
Another test class for this type of configuration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package jackq.springdi.helloworld;
publicclassTest3{
// this is the dependency class required to be injected
Singleton is one of the most common design pattern in Java (as well as other object oriented programming language).
It means that the program or application keeps only one single instance of a class.
If the dependency of one class is a singleton, there is no need to specify which instance should be injected and the
process can be achieved automatically. This is auto-wiring.
Annotation is a language feature in Java which provides a interface to attach extra meta data to
a class, an interface, a method, a parameter, etc. The extra meta data can be accessed by program
at execution time dynamically. Thus, the Spring IoC container can use these meta data to
get the configuration information.
To enable this feature in Spring, the following elements should be added into the XML configuration file.
1
2
3
<!-- namespace content defined as: "http://www.springframework.org/schema/context" -->
<!-- this should be inserted before the definition of the first bean -->
<context:annotation-config/>
Some common annotation are defined by Spring:
@Autowired: autowire a dependency. This can be used on field and constructor argument;
@required: specify that a field is a required dependency. When context or factory is initialized, the
unsatisfied required filed will throw exception to prevent incomplete dependency. This is annotated on
the corresponding setter method;
etc.
Beside the core library of Spring framework, other modules are also use annotation widely, such as the
annotation configuration for MVC class.
The following code is a simple example of @Autowired.
MVC represents Model, View, Controller. Most of web page is generated by populate date from
data and fill them into web page template. Thus, it nature to divide the code of a web site
into three parts: the model which related to data, view which related to html and template
and the controller which related to the process logics.
In Java, the standard model to process web request is the Servlet model. A web
application container like Tomcat, Jetty, etc. manages the low level HTTP protocol details
and maps the HTTP request and response with Java object. A servlet is a handler that perform
the process to fill the response object by data from the request object.
Spring MVC combines the standard Servlet with the MVC pattern. It is implemented as a Servlet
mapped to all of the path and route to specific controller.