Spring framework learning notes for beginners [spring tutorial]

Spring is a widely used framework in the process of Java Web development. It is a lightweight application framework. Its powerful function and excellent performance are loved by many developers.

In the actual project development process, the server is divided into three layers: presentation layer (Web), business logic layer (Service) and persistence layer (Dao). In the current mainstream development framework, Spring has a unique processing method for each layer. For example, in the presentation layer, the integration with struts 2 framework is provided in the presentation layer, and JDBC template technology is provided in the persistence layer, The business logic layer provides transaction management and logging.


1, Composition of Spring framework


  • Lightweight J2EE application development framework
  • Main components: dependency injection container, AOP implementation, DAO/ORM support, Web Integration
  • Core package: the most basic part of the framework, which provides IoC dependency injection features. The basic concept here is BeanFactory, which provides a classic implementation of the Factory pattern to eliminate the need for procedural singleton pattern, and really allows the separation of dependencies and configurations from the programming logic.
  • The Context encapsulation package based on the Core encapsulation package provides a framework object access method, some like JNDI registrar. The characteristics of Context encapsulation package come from Beans encapsulation package, and add support for internationalization (I18N) (such as resource binding), time propagation, resource loading method and transparent creation of Context, such as through Servlet container
  • DAO provides an abstraction layer of JDBC, which can eliminate lengthy JDBC coding and parse database vendor specific error codes. Moreover, the JDBC package also provides a better declarative transaction management method than programming. It not only implements a specific interface, but also applies to all POJO s.
  • The ORM package provides the integration layer of the commonly used "object relationship" mapping API. Using ORM encapsulation package, you can mix all the features provided by Spring for "object relationship" mapping, such as simple declarative transaction management.
  • Spring's AOP package provides Aspect oriented programming in accordance with AOP (Aspect oriented programming).
  • Spring's Web package provides basic integration features for web development. When spring is used with WebWork or struts, this package can be combined with other frameworks.
  • The MVC package in spring provides the model view controller (MVC) implementation of Web applications. Spring's MVC framework provides a clear separation model. Spring's MVC framework provides a clear separation model between domain model code and web form, and can also take advantage of other features of spring framework.

What problems can be solved through video: https://www.bilibili.com/video/BV1nz4y1d7uy​​​​https://www.bilibili.com/video/BV1nz4y1d7uy%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B Video introduction:

This set of Java video tutorials mainly explains the use and application of spring 4 in SSM framework. This set of Java video tutorials covers almost all the knowledge points that may be used in practical work. Lay a solid foundation for future study.


2, The role of Spring framework

In the process of typical application development, developers use Struts, WebWork and other application frameworks to develop front-end programs, use JDO, Hibernate and other persistence layer frameworks for database operation, and use Spring IoC container (control inversion) to configure and manage components in the whole system.


Inversion of Control refers to the transfer of the control right of the program dependency in the traumatic programming mode from the inside of the program to the outside of the program, allowing the programmer to separate the dependence and management of the specific implementation from the program logic.

Control inversion also embodies the design idea of "interface oriented programming" and separating call and implementation.

2.2. Dependency injection

The container dynamically injects certain dependencies between components.

Inversion of control is also called dependency injection (DI)

IoC/DI example:

Dependency injection is the embodiment of "interface oriented programming" in Java programming idea. Therefore, when designing programs, the components that the program depends on often appear in the form of taboos instead of directly using specific implementation classes:

//Design interface
public interface PersonLogic{
String sysHello();
//Use interface
public class Action{
public void execute(){
//Rely on an instance of an implementation class of the interface to complete the processing

2.3 implementation mode of IoC/DI
2.3.1 type 1 interface injection

The caller of a method provides an implementation class (dependent component) of an interface through method parameters

//Using component interfaces in method parameters
public class DemoAction{
public void execute(PersonLogic person){
//The function implementation of Action depends on the Logic object passed through the method parameters
//When the method is called, the dependent component is injected from the outside
DemoAction action = new DemoAction();
PersonLogic person = new PersonLogicEnImpl();

2.3.2 type 2 set value injection

Learn from the design method of JavaBean and pass in other components since the business processing object through setter method

//The dependent components appear in the form of attributes and provide corresponding getter and setter methods
public class DemoAction{
private PersonLogic person;
//Inject dependent components through set value
public void setPerson(PersonLogic person){
this.person = person;
//When calling the method, inject the dependent components from the outside through the setter method
DemoAction action = new DemoAction();
PersonLogic person = new PersonLogicCnImpl();

2.3.3 type 3 structure parameter injection

After the business object is initialized, it will be processed by other methods; After the initialization phase is completed, you can get a complete business object for direct use.

//The dependent components appear in the form of attributes and are provided in the construction method
public class DemoAction{
//The dependent components appear in the form of attributes
private PersonLogic person;
//Obtain the dependent components in the initialization stage through the construction method
public DemoActionType3(PersonLogic person){
this.person = person;
//When calling a method, inject the dependent component from the outside by constructing a method
PersonLogic person = new PersonLogicCnImpl();
DemoAction action = new DemoAction(person);

2.4 comparison of implementation methods of dependency injection
2.4.1 type 1 interface injection

It has a long history and has been used in a large number of container and framework designs, such as servlet, struts 1 and so on

Lack of flexibility and ease of use. At present, Type2 and Type3 have become the mainstream in the development of dependency injection mode

2.4.2 type 2 set value injection

Similar to the traditional java bean pattern, it is more intuitive and natural for programmers to set dependencies through setter method

It avoids the huge and bloated constructor, especially in the case of complex dependencies. There is no need to appear many constructors in the constructor, which is more concise in coding

In some frameworks, there are certain restrictions on the constructor (often requiring a default constructor). At this time, the type 3 method is powerless.

2.4.3 type 3 structure parameter injection

It fully conforms to the java design principle of "creating a complete and legal object in the construction period"

All dependencies are embodied in the construction method

Since no setter method can change the dependency, all components are in a relatively "stable" state after creation. At the same time, it also shields the details of the dependency from the outside world to a certain extent.

For complex dependencies, the order of dependency injection can be determined by the construction method.

3, Apply
3.1 package required


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <bean id="fruit" class="com.it.spring.type1.Banana"></bean>
  <bean id="song2" class="com.it.spring.type2.Song">
    <property name="songName" value="Little apple"></property>
  <bean id="sing" class="com.it.spring.type2.Sing">
    <property name="song" ref="song2"></property>
  <bean id="song3" class="com.it.spring.type3.Song">
    <property name="name" value="Little apple"></property>
  <bean id="sing3" class="com.it.spring.type3.Sing">
    <constructor-arg index="0" name="song" ref="song3"></constructor-arg>

4, Summary

  • The emergence of Spring framework
  • Spring framework consists of dependency injection container, AOP implementation, DAO/ORM support, Web integration and other parts.
  • Spring configures and manages the components in the whole application system in the application
  • IoC transfers the control of the program dependency in the traditional programming mode from the inside of the program to the outside of the program, and separates the dependence and management on the specific implementation from the program logic.
  • Among the implementation methods of DI, interface injection appeared first, but due to the lack of flexibility and ease of use, IoC field is no longer the mainstream choice.
  • Type2 set value injection and Type3 construction parameter injection are the mainstream methods at present, but they also have their own advantages in application due to different implementation methods.


Keywords: Java Spring Spring Boot

Added by error_22 on Sat, 12 Feb 2022 04:49:57 +0200