关于Bean的两种常用的作用域

singleton和prototype是最常用的两种作用域,通过@Scope注解来实现

作用域描述
singleton默认的作用域,使用singleton定义的Bean在Spring容器中只有一个Bean实例
prototypeSpring容器每次获取prototype定义的Bean,容器都将创建一个新的Bean实例

下面通过一个例子分析

  1. singleton作用域(默认)
    创建TestController类
package annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import annotation.service.TestService;

@Controller("testController")
public class TestController {

	public int a;//声明实例变量a
	
	public void add(int a) {
		this.a=a;
		System.out.println("令a等于 "+this.a);
	}
}

创建TestAnnotation类

package annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import annotation.controller.TestController;
import annotation.dao.TestDaoImpl;

public class TestAnnotation {
	
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		AnnotationConfigApplicationContext appCon= new AnnotationConfigApplicationContext(ConfigAnnotation.class);
		TestController tc1 = (TestController)appCon.getBean("testController");
		TestController tc2 = appCon.getBean(TestController.class);
	    System.out.println("tc1的地址:"+tc1);
	    System.out.println("tc2的地址:"+tc2);
	    tc1.add(1);
	    tc2.add(0);
	    System.out.println("实例tc1中a的值是"+tc1.a);
	    System.out.println("实例tc2中a的值是"+tc2.a);
		appCon.close();
	}
	
}

运行结果
在这里插入图片描述

tc1与tc2地址相同,说明是同一个Bean实例(一个类中只有一个实例)
先给a赋值为1,然后赋值为0,由于tc1和tc2只是同一个实例的两个不同别名,所以最终a的值都为0

  1. prototype作用域
    创建TestController类
package annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import annotation.service.TestService;

@Controller("testController")
@Scope("prototype")
public class TestController {

	public int a;
	
	public void add(int a) {
		this.a=a;
		System.out.println("令a等于 "+this.a);
	}
}

创建TestAnnotation类

package annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import annotation.controller.TestController;
import annotation.dao.TestDaoImpl;

public class TestAnnotation {
	
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		AnnotationConfigApplicationContext appCon= new AnnotationConfigApplicationContext(ConfigAnnotation.class);
		TestController tc1 = (TestController)appCon.getBean("testController");
		TestController tc2 = appCon.getBean(TestController.class);
	    System.out.println("tc1的地址:"+tc1);
	    System.out.println("tc2的地址:"+tc2);
	    tc1.add(1);
	    tc2.add(0);
	    System.out.println("实例tc1中a的值是"+tc1.a);
	    System.out.println("实例tc2中a的值是"+tc2.a);
		appCon.close();
	}
	
}

运行结果
在这里插入图片描述
显然tc1与tc2的地址不同,是两个不同的实例
故最终a的值不相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一身都是月儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值