栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

下面通过一个例子分析

    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

    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的值不相同

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/703759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号