Creation and use of Spring core project

Table of contents

 1. Create a Spring project

1. Create a Maven project

2. Add Spring dependency 

3. Add startup class

 2. Store objects in Spring

Create a Bean object

Register the Bean object with Spring

3. Take out the bean object from Spring

Case Analysis

        getBean method analysis

some supplements

4. Related interview questions

V. Summary 


After the previous blog, we already know that Spring is an IoC container with many tools and methods, since it is a container. Then there are these two basic functions.

  • store the object in the container
  • remove the object from the container

In Spring, objects are also called Beans, so we will call them Beans when we encounter objects later.

Therefore, the creation and use of the entire project has the following three steps:

1. Create a Spring project

2. Store beans

3. Take Bean

 

 1. Create a Spring project

1. Create a new normal Maven project

2. Add dependencies - Spring framework support (spring-context, spring-beans)

3. Add the startup class (because we are a Spring core project here, not a web project, we need to manually create the startup class by ourselves)


1. Create a Maven project

The essence of Spring is actually a Maven project. Creating a Maven project is somewhat similar to the creation of our previous servlet project.

 

 

2. Add Spring dependency 

 Add Spring dependency in pom.xml in the project (can be copied directly, dependency can be obtained from Maven central warehouse)

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

 


3. Add startup class

 


 2. Store objects in Spring

Overview:

1. Create a Bean object

2. Register the Bean object with Spring (using the Spring configuration file for registration)

Create a Bean object

The so-called Bean is an ordinary object in the Java language
 

 


 

Register the Bean object with Spring

First of all, we register the configuration file to use Spring, and register in the configuration file.

spirng configuration file (can be copied directly)

        <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

But note that our configuration file here is not the final version - when we use annotations to access Bean objects more efficiently later, our configuration file will change.

 

As shown below

 


3. Take out the bean object from Spring

Case Analysis

 

getBean method analysis

 

 It can be seen from the above figure that the getBean method can pass multiple parameter types.

So is there any difference between these different parameter types? Let's take a look together!

method one:

Sometimes there may be some problems due to the need for forced transfer

Method Two: 

 Method 3 (This is also the method I recommend everyone to use)


 

some supplements

The same User class is registered twice in spring

 

 

 singleton pattern

 

4. Related interview questions

In addition to ApplicationContext, we can also use BeanFactory as the context of Spring

 ApplicationContext and BeanFactory have the same effect, and ApplicationContext belongs to the subclass of BeanFactory .
Their differences are as follows.

 

V. Summary 

 

Guess you like

Origin blog.csdn.net/weixin_61061381/article/details/127701734