Spring

Inversion Of Control (IOC)

  • If class A depend on class B , pass the declaration of class B to third party to declare

  • Example: If a graduate want to find a suitable job , he should find the agent to help him to look for his suitable job according to his abilities, instead of every student find for the same job.

  • Dependency injection is a one of the example of inversion of control

  • Without IOC

public class Graduate {
    private Programmer programmer;
}
// That assume all the graduate must find a programmer job, which is strongly coupled
  • With IOC

public class Graduate{
    private Job job;
    public void setJob(Job job){
        this.job = job;
    }
}

// The job class is an interface;
// The job of the graduate will handled by another class(e.g Main class)
// which make the graduate loosely coupled with Job class

Dependency Injection

Initial

Dependency Injection (by constructor)

Dependency Injection (by setter)

  • Initially, lesson class is fixed with a specific student class. If we declare new Lesson class, the student must be the same as every lesson class, which will cause out coupling.

  • By using dependency injection, lesson class and student will be independent to each other.

Why Spring?

  • It has strong IOC container (Application Context) which manage and create the beans

  • Developer can do dependency injection easily by using the bean of container

Structure of Java Spring

Data Access Integration

  • Provide several popular API , such as hibernate, JPA

  • Make the syntax of connecting database simpler

Web

  • Provide several web function, such as file reader

  • Define Controller, Model and View

Core

  • Application Context Container or Bean Factory Container is a container to contain the bean factory which is a xml file traditionally

  • Application Context Container

  • Bean Factory Container

Bean

Introduction

  • Bean can be instantiated as a instance of the class, which is similar with the below syntax to declare the new instance - sample Student

  • Bean can rely on another beans by using auto wire

  • Example of creating bean by using traditional xml

Scope

Type

  • There are 5 types of scope - singleton, prototype, request, session and global session

  • Singleton: This scopes the bean definition to a single instance per Spring IoC container (default)

  • Prototype: This scopes a single bean definition to have any number of object instances.

  • Request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring Application Context.

  • Session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring Application Context.

  • Global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring Application Context.

Singleton vs Prototype

  • Singleton

Output

  • Prototype

Output

Life Cycle

  • Initialize and Destroy

  • we can add custom event for each as a listener

Output:

Dependency Injection

  • By constructor

  • By setter

Auto Wire

By Type / By Name (XML)

  • In text editor, it will look for the bean containing spell checker type or the same name automatically and inject into text editor

By Type (Annotation)

  • In text editor, it will look for the bean containing spell checker type automatically and inject into text editor

  • If spellchecker is a interface , make sure that it should be implemented by one class only

By Name (Annotation)

  • If a spell checker may be a interface and implement by several class, so we should look for the bean according to the name of the bean

Configuration

In spring, we can make good use of annotation instead of configure xml file

Annotation

XML

Component Scanning

Introduction

  • Initially, we need to declare each bean one by one. Now, we can make good use of annotation to mark the class as a component, and perform scanning on spring container which can be in xml or java code , that will register the bean automatically for class who is marked as a component

  • There are common 5 annotations to create beans

Component

  • It can be used in different layers

  • Not recommended to use

Service

  • It is one part of the component

  • Mainly responsible for the business logic

  • In the most situation, it should be a interface and implemented by different class

Repository

  • It is one part of the component

  • Mainly responsible for visiting database and locate in the dao folder

Controller

  • It is one part of the component

  • used to handle URL and can used to return jsp file

  • Example 1 (Return jsp File)

  • Example 2 (return string or other type)

RestController

  • It is one part of the controller

  • act as a api end point and must be return in string or other type

  • RestController = Controller + ResponseBody

Last updated

Was this helpful?