Spring project creation and Spring Bean storage and reading

Table of contents

1. Create a Spring project

1.1 Create a Maven project

1.2 Add Spring framework dependencies

1.3 Add startup class

Second, the creation and storage of Bean objects

2.1 Create Bean

2.2 Register the bean to the container

2.3 Get and use the Bean object

2.3.1 Create a Spring context

2.3.2 Get the Bean object from the Spring containerEdit

Extension (multiple ways to obtain beans):

2.3.3 Using beans

2.3.4 ApplicationContext VS BeanFactory (common interview questions)

3. Summary


Preface: We know that Spring is an IoC container that contains many tools and methods. Since it is a container, it has

The two most basic functions:

  • Store the object into the container (Spring);
  • Get the object out of the container.

1. Create a Spring project

Next, use the Maven method to create a Spring project. Creating a Spring project is similar to Servlet. It is divided into the following 3 steps:

  1. Create a normal Maven project.
  2. Add Spring framework support (spring-context, spring-beans).
  3. Add startup class.

In the Java language, objects are also called Beans, so when we encounter objects later, they are called Beans.

1.1 Create a Maven project

 When the directory becomes as shown below, the Maven project is initialized.

 

1.2 Add Spring framework dependencies

Download Spring Context dependencies from the central repository:

1.3 Add startup class

Finally, create a startup class under the Java folder of the created project, including the main method:

Second, the creation and storage of Bean objects

2.1 Create Bean

The so-called Bean is an ordinary object in the Java language, and the implementation code is as follows:

2.2 Register the bean to the container

 Add the Spring configuration file spring-config.xml to the created project, and place this file in the root directory of resources:

The fixed format of the Spring configuration file is as follows (no need to remember):

<?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>

Next, register the Student object in Spring. The specific operation is as follows:

2.3 Get and use the Bean object

Obtaining and using the Bean object is divided into the following three steps:

  1. Get the Spring (context) object, because the objects are all managed by Spring, so you need to get the object from Spring, then get the Spring context first.
  2. Get a specified Bean object through Spring (context)
  3. Use the Bean object

If multiple beans are to be obtained, repeat steps 2 and 3 above.

2.3.1 Create a Spring context

Use ApplicationContext to create a Spring context:

 It should be noted that the path here must be the same as the name of the Spring configuration file, otherwise it cannot be created successfully.

2.3.2 Get the Bean object from the Spring container

Extension (multiple ways to obtain beans):

1. Obtain by name:

2. Get by type:

It should be noted that if a type stores multiple instances in Spring, then an error will be reported when using the type to obtain the Bean:

If a class is stored twice in the bean, are the two bean addresses the same? Let's verify:

Therefore, the addresses of the two different bean instances created here are different, and the addresses will not be the same because they store beans of the same class.

3. Obtain by name + type:

2.3.3 Using beans

import com.spring.demo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        //1.得到Spring的上下文对象,创建的时候需要配置 Spring配置 的信息
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Spring-config.xml");
        //2。从Spring容器中获取到Bean对象
           Student student = (Student)context.getBean("student");
           //3.使用Bean(非必须的)
           student.sayHi();
    }
}

operation result:

2.3.4 ApplicationContext VS BeanFactory (common interview questions)

In addition to ApplicationContext, we can also use BeanFactory as the context of Spring, as shown in the following code:

We found that it seems that in the current scenario, ApplicationContext and BeanFactory have the same effect, because ApplicationContext belongs to the subclass of BeanFactory:

Since there is an inheritance relationship between the two, let's take a look at the differences between the two:

Let's first create a Teacher class under the com.spring.demo package:

 And under the Spring configuration file, save the Teacher in Spring:

Observe the difference between the two:

It can be found that Application loads and initializes all Bean objects at one time, while BeanFactory is lazy loading, which is loaded only when it is needed, so BeanFactory is lighter.

Summary of the difference between ApplicationContext and BeanFactory

  • In terms of inheritance relationship and functionality: the Spring container has two top-level interfaces: BeanFactory and ApplicationContext. Among them, BeanFactory provides the basic ability to access the container, and ApplicationContext belongs to the subclass of BeanFactory. In addition to inheriting all the functions of BeanFactory, it also has unique features, and also adds support for internationalization and resource access. support, and event propagation support.
  • In terms of performance: ApplicationContext loads and initializes all Bean objects at one time, while BeanFactory loads that one when it needs it, so it is more lightweight, so ApplicationContext consumes more memory, and it is loaded once, but in the future The reading speed will be very fast, and because BeanFactory needs to save memory, it will load and initialize beans when calling, so the efficiency is not high.

3. Summary

a. Create a Spring project

  1. Create a maven project
  2. Add Spring-context dependency
  3. Create a startup class (in preparation for obtaining beans from the Spring container later)

b. Storage Bean

  1. Create the Bean object first
  2. Through the Spring configuration file (spring-config.xml) <bean id="bean name" class="package name + class name"></bean>

c. Read beans

  1. Get the Spring object (ApplicationContext, BeanFactory)
  2. Get the Bean object through the Spring object getBean method [DI operation]
  3. Optional: use beans

Guess you like

Origin blog.csdn.net/qq_63218110/article/details/130169033