Spring(9):通过注解设定Bean的作用域

一、作用域

Spring的bean作用域包含Singleton、prototype、web(request、session、application、websocket)等作用域,当前以多例模式为例,展示使用方法。

二、环境

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>

三、具体实现

1.prototype

(1)TestBean类
设置包名称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类设置内部Bean,id为testBean2,同时设置为多例模式

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)单元测试

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);
        }
    }
}

结果:

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

 

上一篇:手写web服务器:定义Configuration和Bean注解,实现更灵活的类注入


下一篇:spring boot 入参方式