ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-27 09:03:41  阅读:170  来源: 互联网

标签:ioc 作用域 Spring testBean2 testBean1 Bean spring TestBean com


一、作用域

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

 

标签:ioc,作用域,Spring,testBean2,testBean1,Bean,spring,TestBean,com
来源: https://blog.csdn.net/u010886217/article/details/102763658

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有