Spring (9): Bean scope set by notes

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u010886217/article/details/102763658

First, the scope

Spring bean scope comprises Singleton, prototype, web (request, session, application, websocket) and other scopes, current multi-mode embodiment, for example, display usage.

Second, the environment

1.Pom

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <!--单元测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Third, the specific implementation

1.prototype

(1) TestBean class
set package name and scope of cases of testBean2

package com.spring.ioc;

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

/**
 * Created by Administrator on 2019/10/27.
 */
@Component("testBean2")
@Scope("prototype")
public class TestBean {
}

(2) TestConfiguration class provided inside Bean, id is testBean2, while multiple embodiments set mode

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * Created by Administrator on 2019/10/27.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {

    @Bean("testBean1")
    @Scope("prototype")
    public TestBean testBean(){
        return new TestBean();
    }
}


(3) Test Unit

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/27.
 */
public class TestCode {
    @Test
    public void test(){
        ApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);

        for (int i = 0; i < 10; i++) {
            TestBean testBean1=context.getBean("testBean1",TestBean.class);
            System.out.println("testBean1 = " + testBean1);
        }
        System.out.println("============================");
        for (int i = 0; i < 10; i++) {
            TestBean testBean2=context.getBean("testBean2",TestBean.class);
            System.out.println("testBean2 = " + testBean2);
        }
    }
}

result:

testBean1 = com.spring.ioc.TestBean@3e3047e6
testBean1 = com.spring.ioc.TestBean@37e547da
testBean1 = com.spring.ioc.TestBean@2b6856dd
testBean1 = com.spring.ioc.TestBean@5db45159
testBean1 = com.spring.ioc.TestBean@6107227e
testBean1 = com.spring.ioc.TestBean@7c417213
testBean1 = com.spring.ioc.TestBean@15761df8
testBean1 = com.spring.ioc.TestBean@6ab7a896
testBean1 = com.spring.ioc.TestBean@327b636c
testBean1 = com.spring.ioc.TestBean@45dd4eda
============================
testBean2 = com.spring.ioc.TestBean@60611244
testBean2 = com.spring.ioc.TestBean@3745e5c6
testBean2 = com.spring.ioc.TestBean@5e4c8041
testBean2 = com.spring.ioc.TestBean@71c8becc
testBean2 = com.spring.ioc.TestBean@19d37183
testBean2 = com.spring.ioc.TestBean@1a0dcaa
testBean2 = com.spring.ioc.TestBean@3bd40a57
testBean2 = com.spring.ioc.TestBean@fdefd3f
testBean2 = com.spring.ioc.TestBean@d83da2e
testBean2 = com.spring.ioc.TestBean@a4102b8

 

Guess you like

Origin blog.csdn.net/u010886217/article/details/102763658