This is a reading note from Spring Framework 3.1 Tutorial pdf, created by tutorialspoint. This pdf contains information for Spring 3 Core Basics, which is very useful for understanding defination and practive in Spring programming.
Note: All tips that is written by book will has a sign near it.
All code example in this post comes from book.
This note includes Chapter 1 to Chapter 10 in tutorial.
Chapter 2: Spring Framework Architecture
Spring is a modular framework, which means you can pick the modules that only fits your need. Basic structure of Spring framework is following. There are three main componets:
Data Access Integration and
Web (MVC/Remoting). Other important componets include
Core Container contains:
Context module and
Expression Language module:
Core: fundamental of framework, including
Bean Factory, implementing factory pattern.
Context: based on
Beansmodule. It is a medium to access any objects defined and configured. The
ApplicationContextinterface is the focal point of the
Expression Language: provides language for querying and manipulating an object graph at run time.
Data Access/Integration contains:
JMS module and
JDBC: provides a JDBC-abstraction layer that can simplify JDBC coding
ORM: provide integration for popular object-mapping APIs, including
OXM: provide integration for Object/XML mapping, such as
JSM: Java Messaging Service. It can producing and consuming messages.
Transaction: contains programmatic and declarative transaction management for classes and POJOs.
Web-Struts module and
Web: provide important functions for web development, such as
multipart file uploaderand
servlet listenersand a web-oriented
Web-Servlet: provide Spring
Web-Structs: provide support for Structs
MVCimplemention to be used in a portlet environment.
aspect-orientedprogramming implementation, allowing you to define method-interceptor or pointcuts to cleanly decouple code that implements functionality that should be seperated.
Aspectsmodule provides support for
Instrumenetationmodule provide class instrumentation support and class loader implementation for certain application servers.
Test: provides testing support for
Chapter 5: Spring Ioc Containers
The main function of Spring IoC Container is to create objects, wire objects together, configure objects and manage objects in their lifecyle.
Spring IoC containter uses
dependency injection/DI to manage objects, which are called spring
Container gets information from the
Java annotation or
This is the process of Spring IoC container works:
Spring has two distinct type of containers:
BeanFactory: this is the simplest container from DI. Most commonly used implementation is
XmlBeanFactoryclass. It reading the configuration metadata from
xmlfile and use it. Example:
ApplicationContext: More advanced and widely used container compare to the one above. This includes some enterprise-function. Most common implementations are:
FileSystemXmlApplicationContext: loads the definations of the beans from
xmlfile. Note you need to provide full path of xml bean configuration file path. Example:
ClassPathXmlApplicationContext: example is in
BeanFactory Container. You do not need full path of xml file but you need Class Path to let spring find xml configuration file.
WebXmlApplicationContext: loads all beans with in a
webapplication from xml.
Chapter 6: Spring Bean Definition
Beans are objects that are initialized, assembled, and otherwise managed by Spring IoC Container. They are created with the
configuration metadata that supplied by
<bean/> defination from
Configuration MetaData provide following:
- How to create a bean
- Bean’s lifecycle details
- Bean’s dependencies
class: mandatory. Give the java class that is used to create beans.
name: specifies the bean
identifieruniquely. In an
xml, you need
nameto specify the bean identifiers.
scope: specifies bean scope. See notes in chapter 7.
constructor-arg: for inject the dependencies. See notes in chapter 8
properties: for inject the dependencies. See notes in chapter 8
autowiring mode: for inject the dependencies. See notes in chapter 8
lazy-initialization mode: tells the IoC container to create a bean instance when it is firstly requested, rather than at startup.
initialization method: a
callbackto be called just after all
propertiesfor the bean have been set by container. See notes in Chapter 8.
destruction method: a
callbackto be used when the
containercontaining the bean is destroyed. See notes in chaper 8.
Configuration Metadata can be provided by:
xmlbased configuration file.
Example of Bean Properties from book:
Chapter 7: Spring Bean Scopes
You can declaring scope of beans when you creates
<bean/> in xml file. There are five scopes that Spring support:
singleton: Default. Spring IoC container cretes exactly one instance of the object defined by that bean defination. This object is stored in a cache of such singletion beans. A request will return the cached object. Example:
prototype: create new bean instance of object every time a request for the specific bean is made.
As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.
Chapter 8: Spring Bean Life Cycle
initialization work can be done inside
In xml, you can use
init-method attribute to call a self-defined method when a bean instance is created. Then add this method in your bean java class.
destruction can be done insdie
In xml, you can use
destroy-method attribute to call a self-defined method. You need to add this method in your bean java class.
You can also put
defalut-destory-method attributes in the main bean in
xml file, such as
<bean xmlns="..." xmlns:xsi="..."></bean>, to let all beans use same initialization method or destruction method.
Chapter 9: Spring Bean Post Processors
BeanPostProcessor interface defines callback methods that you can implement to provide your own instantiation logic, dependency-resolution logic, etc.
You can configure multiple BeanPostProcess interfaces. You can control the order of them.
Spring IoC container instantiates a bean instance and then BeanPostProcessor works.
ApplicationContext automatically detect beans with BeanPostProcessor implementation and register these beans as post-processors. These processors will be automatcally called when bean is created.
Chapter 10: Spring Bean Definition Inheritance
parent bean can be inherited by a
child bean. Child bean can override parent bean’s attributes.
parent attribute should be used in
xml file to define a parent bean in child bean.
You can create
Bean Defination Template in
xml file if you need a general parent bean for all childs beans. You can not use
class attribue in bean template. You must define
abstruct attribute as true. Example: