Zero basic learning spring framework

Spring framework

1. spring official website

Official website:

2. Spring framework

Framework: a series of jar packages and the programming method for realizing a certain function are agreed at the same time, which may be different from the original programming method! Each framework solves some specific problems!

Problems solved by Spring framework: it solves the problem of creating and managing objects and realizes decoupling!

The simple syntax for creating objects is roughly as follows:

User user = new User();

Using the above methods to create objects is not easy to realize decoupling!

Decoupling: decoupling between objects! That is, reduce the dependency between objects!

Dependency: if you need to use another object in the working process of one object, it is called dependency on another object!

For example:

public class UserJdbcDao {
	public void login() {
		// Connect database
		// Execute SQL statement

public class UserLoginServlet {
	public UserJdbcDao userJdbcDao = new UserJdbcDao();
	public void doPost() {

In the above code, UserLoginServlet depends on UserJdbcDao!

Based on the above code, if the technology used by UserJdbcDao needs to be replaced, or for other reasons, this class no longer meets the needs of the project and needs to be replaced with another class or implemented with another technology, a new UserMyBatisDao may be created to replace the original UserJdbcDao!

If replacement is required, public UserJdbcDao userJdbcDao = new UserJdbcDao(); It needs to be replaced by:

public UserMyBatisDao userMyBatisDao = new UserMyBatisDao();

If multiple Servlet components in this project use UserJdbcDao, they need to be replaced multiple times!

Therefore, if you want to replace UserJdbcDao with UserMyBatisDao, there may be more code to adjust in the whole project! Usually, it is called "too high coupling"!

First, you can define an interface:

public interface UserDao {
	void login();

Then, make UserJdbcDao and UserMyBatisDao implement the above interfaces! Then, when DAO objects need to be used later, they can be declared as:

// Use the interface declaration to create an object that implements a class
public UserDao userDao = new UserJdbcDao();

The declaration statement of the object does not need to be adjusted when replacement is needed later!

Next, you can also use the factory pattern in design pattern to solve the problem of creating objects:

public class UserDaoFactory {
	public static UserDao newInstance() {
		return new UserJdbcDao();

Then the previous code can be further adjusted to:

public UserDao userDao = UserDaoFactory.newInstance();

In any Servlet, you can use the above code when you need to use DAO. There is no implementation class name in the above code. If you need to replace the implementation class, the above code does not need to be adjusted at all! Only the return value of the method in the factory class needs to be adjusted!

You can also see that in the Servlet class, the name of a DAO class does not need to appear at all, so it can be called UserLoginServlet, which does not depend on UserJdbcDao or UserMyBatisDao! This removes the coupling between UserLoginServlet and UserJdbcDao or UserMyBatisDao!

In the above practice, we use interfaces and factory classes, which are the core of decoupling! However, when actually developing a project, it is impossible to create corresponding interfaces and factory classes for a large number of classes! Therefore, the Spring framework appears! You can simply understand the Spring framework as a large factory, which is specially used to create and manage the objects of classes required in the project. When you need to use an object, you can get it from the Spring framework, instead of creating objects and writing factories by yourself!

3. Use Spring to create objects and get objects from Spring through the parameterless construction method

First, create Maven Project. During the creation process, check Create a simple Project and fill in CN with Group Id Tedu, Artifact Id, fill in Spring01, Packaging and select war.

If you only use Spring, you don't need to choose war for the above Packaging, and you can choose jar!

The created project does not have a web by default XML file, you need to create this file, otherwise, the project will report an error!

After the creation is completed, you need to open the POM Add org. XML Spring webmvc dependency of spring framework:


If you only use the content of the Spring framework, you can add Spring context dependency instead of Spring webmvc. Since you will learn the Spring MVC framework later and include Spring context in Spring webmvc, you can also use Spring webmvc directly.

The dependencies added this time can be as long as they are version 4.2 or above! The optional versions are:,, etc.

Then, from Download the Spring configuration file and extract it to get ApplicationContext XML file and copy the file to src/main/resources of the project.

Suppose you need to create an object of Date class through Spring, then in ApplicationContext Add configuration to XML:

<!-- Acquisition needs Date Object of class -->
<!-- id Property: name. The object will be obtained according to this name -->
<!-- class Property: which class of object is created -->
<bean id="date" class="java.util.Date"></bean>

Finally, create a runnable class and add code:

ClassPathXmlApplicationContext ac
		= new ClassPathXmlApplicationContext(
Date date = (Date) ac.getBean("date");

Run to see the date and time of the output.

The above practice requires that this class has no parameter construction method!

4. Create and obtain objects through static factory method [not commonly used]

Some classes may not have parameterless construction methods, but there may be a method modified by static that returns objects of the current type. For example, in Calendar Class:

public static Calendar getInstance() { ...

The above getInstance() method is the static factory method of Calendar class!

If you need to use Spring to manage Calendar objects, you need to use ApplicationContext Configuration in XML:

<!-- 2. Creating objects through static factory methods -->
<!-- factory-method Property: the name of the factory method -->
<bean id="calendar" class="java.util.Calendar"

5. Create and manage objects through instance factories [not commonly used]

Suppose a class does not meet the above two methods to create objects, and there is another class in which a method can create objects, for example:

public class UserDao {
	public UserDao(String s) {

public class UserDaoFactory {
	public UserDao newInstance() {
		return new UserDao(null);

The configuration is:

<!-- 3. Create objects through instance factory methods -->
<!-- factory-bean Properties: of factory objects bean-id -->
<!-- factory-method Property: the name of the factory method -->
<bean id="userDaoFactory" 
<bean id="userDao" class="cn.tedu.spring.UserDao"

6. Scope of objects managed by Spring [only know]

Objects managed by Spring are singleton by default!

Single example: at the same time, there must be only one object of a class!

If you need to configure whether the object is a singleton, you can:

<!-- scope Property: the scope of the object of the configuration class. Whether it is a singleton or not. The default value is singleton´╝îWhen configured as prototype Time indicates non singleton -->
<bean id="user" class="cn.tedu.spring.User"
	scope="prototype" />

If the singleton mode is designed by ourselves, it can be, for example:

public class User {

	private static User user = new User();

	private User() {

	public static User getInstance() {
		return user;


Because the obtained objects are decorated with static, the objects of the singleton class are static, that is, they have the characteristics of resident memory! The scope of the singleton object is that the whole program starts loading this class and ends running the program! The scope of non singleton objects is the scope of ordinary variables!

In addition, the singleton mode is also divided into lazy singleton and hungry singleton. If it is lazy singleton mode, the object will be created only the moment before obtaining the object. If it is hungry singleton mode, the object has been created at the beginning of the project!

By default, all singleton objects managed by Spring are hungry! You can add configurations in the Spring configuration file:

<!-- lazy-init Attribute: whether to load lazily. The value can be Boolean -->
<bean id="user" class="cn.tedu.spring.User"
	lazy-init="true" />

Note: it must be based on the singleton mode before discussing whether to load lazily!

Implementation code of lazy singleton:

public class User {

	private static User user;
	private static final Object lock = new Object();

	private User() {

	public static User getInstance() {
		if (user == null) {
			sychronized(lock) {
				if (user == null) {
					user = new User();
		return user;


7. Life cycle of objects managed by Spring [not commonly used]

The life cycle of an object is the process from creation to destruction.

Take Servlet as an example. During the process from creation to destruction, init(), service(), destroy() methods are also executed. init() method is the initialization method that will be executed immediately after the object is created, and it will only be executed once, while service() method is the method that will be executed every time the request is received, which may be executed several times, destroy() method is a method executed immediately before the object is destroyed, and it will only be executed once!

Servlet components in Java EE are created and managed by containers such as Tomcat! As a programmer, you must know which methods are called at what time before you can rewrite these methods correctly, so as to decide "when and what to do"!

When the Spring framework is used, the management of objects is also handed over to the framework! In order to ensure that the tasks to be executed can be customized during initialization and resources can be released before destruction, the corresponding life cycle method can also be declared in the class:

public class User {

	public User() {
		System.out.println("establish User Object!");
	public void onCreate() {
	public void onDestroy() {

During configuration, add the configuration of init method and destroy method attributes:

<!-- init-method: The name of the initialization method of the lifecycle method -->
<!-- destroy-method: The name of the destruction method for the lifecycle method -->
<bean id="user" class="cn.tedu.spring.User"
	destroy-method="onDestroy" />

Keywords: Java Spring Spring Boot

Added by ctcmedia on Thu, 10 Feb 2022 05:29:35 +0200