(Respect the fruits of labor, reproduced please note: juejin.cn/post/684490…

preface

Today I am going to start a series of articles on Spring, which will mainly document some tips encountered in the process of learning and using Spring, so I named it “Talking About Spring again”.


The body of the

Spring is a lightweight IOC and AOP container framework. Is a set of frameworks that provide foundational services for Java applications. It is intended to simplify enterprise application development by allowing developers to focus only on business requirements.

Spring is a low-intrusion design with minimal code contamination. In addition, Spring’s inversion of control mechanism leaves the dependencies between objects to the framework, reducing the coupling of components. IOC makes it easy to create individual beans and inject them where you need them.

Today let’s take a look at the scope of Spring beans, using the easy-to-use SpringBoot.

SpringBoot is a sub-project of Spring open source organization. It is a one-stop solution for Spring components. It mainly simplifies the difficulty of using Spring, saves heavy configuration, and provides various initiators for developers to get started quickly.

Next, let’s build a SpringBoot project together

The project catalog is as follows:

package com.ywq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/** * created by yangwenqiang on 2019-11-30 */

@SpringBootApplication
public class StartApplication {
    // Start the SpringBoot service
    public static void main(String[] args) { SpringApplication.run(StartApplication.class,args); }}Copy the code

TestController:

package com.ywq.controller;

import com.ywq.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/** * created by yangwenqiang on 2019-11-30 */
@RequestMapping("/test")
@RestController
public class TestController {

    @RequestMapping(value = "/scopeTest", method = RequestMethod.GET)
    public String testScope(a){

        return "This is Scope Test!"; }}Copy the code

TestService:

package com.ywq.service;

import org.springframework.stereotype.Service;

/** * created by yangwenqiang on 2019-11-30 */

@Service
public class TestService {

    // Any business processing logic can be defined below
    / /...
}

Copy the code

Configuration file application.properties:

server.port=8632
Copy the code

Maven configuration file pom.xml:

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > <! -- Specify parent's starter, <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 2.2.1. RELEASE < / version > < / parent > < groupId > com. Ywq < / groupId > < artifactId > beans - scope < / artifactId > < version > 1.0 - the SNAPSHOT < / version > <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <! <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Next, we can run the main method directly to start the project:

Well, here we simply set up a SpringBoot project, next, we will do a simple introduction to Spring Bean scope ~

The Spring framework supports the following five scopes for beans:

  • Singleton: Beans have only one instance per Spring IOC container
  • Prototype: A bean definition can have multiple instances.
  • Request: Each HTTP request creates a bean, which is only valid in the Web-based Spring ApplicationContext context.
  • Session: In an HTTP session, one bean definition corresponds to one instance. This scope is valid only in the case of web-based Spring ApplicationContext.
  • Global-session: In a global HTTP session, one bean definition corresponds to one instance. This scope is valid only in the case of web-based Spring ApplicationContext. The default Spring bean is scoped to Singleton

Singleton is Spring’s default Bean scope, meaning that there is only one current object in the current IOC container.

Next, let’s do a simple verification:

Modify TestController:

package com.ywq.controller;

import com.ywq.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/** * created by yangwenqiang on 2019-11-30 */
@RequestMapping("/test")
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private TestService testService2;

    @RequestMapping(value = "/scopeTest", method = RequestMethod.GET)
    public String testScope(a) {
        // Check whether the two instances of TestService we inject are the same
        returnString.valueOf(testService == testService2); }}Copy the code

The following output is displayed:

When we specify that we are currently using multiple instances, we can annotate the Bean when we define it, as follows:

package com.ywq.service;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/** * created by yangwenqiang on 2019-11-30 */

@Service
@Scope("prototype")
public class TestService {

    // Any business processing logic can be defined below
    / /...
}

Copy the code

The following output is displayed:

When we annotate the Bean with @scope (” Singleton “), we see that the result changes to true, verifying that the default Scope is indeed a Singleton.

Next, I will continue to update the use of Spring in the process of some small points, I hope you can continue to pay attention to ~


If it is helpful to you, remember to like ha, welcome everyone to pay attention to my blog, pay attention to the public number (Wenqiang’s technology cabin), learn more technical knowledge, travel with the ocean of knowledge ~