Spring攻略笔记-4 Bean的作用域

Bean的作用域即当前创建的Bean在能在什么范围使用,每次请求是重新创建Bean还是使用已经创建好的Bean。它是在配置Bean的时候使用scope来设定的,有以下几种

singleton:每个Spring Ioc容器中创建一个Bean实例,即在整个程序中就一个实例。

Prototype:每次请求时创建一个新的Bean实例

Request:为每次HTTP请求创建一个Bean实例,仅在Web应用上下文有效

Session:为每次HTTP会话创建一个Bean实例,仅在Web应用上下文有效

GlobalSession:为每个全局HTTP会话创建一个Bean实例,仅在门户应用上下文中有效


默认情况为singleton


比如创建一个学生类,它的作用域为默认,使用注解的方式,即不使用@Scope

package com.lkt.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
public class Student {
	private String userName;
	private String password;
	private String realName;
	@Autowired
	private Clazz clazz;
	
	public Clazz getClazz() {
		return clazz;
	}
	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(String userName,String password,String realName) {
		this.userName=userName;
		this.password=password;
		this.realName=realName;
	}
//	@Override
//	public String toString() {
//		
//		return "userName:"+userName+"  password:"+password+"  realName:"+realName;
//	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
}


测试创建该实例时

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Student s1=(Student)ac.getBean("student");
		System.out.println("s1:"+s1);
		Student s2=(Student)ac.getBean("student");
		System.out.println("s2:"+s2);
		
		System.out.println(s1==s2);
		//System.out.println(student.getClazz().getClassName() );
	}


结果:

s1:com.lkt.entity.Student@4b142196
s2:com.lkt.entity.Student@4b142196
true


如果使用prototype作用域

package com.lkt.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Student {
	private String userName;
	private String password;
	private String realName;
	@Autowired
	private Clazz clazz;
	
	public Clazz getClazz() {
		return clazz;
	}
	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(String userName,String password,String realName) {
		this.userName=userName;
		this.password=password;
		this.realName=realName;
	}
//	@Override
//	public String toString() {
//		
//		return "userName:"+userName+"  password:"+password+"  realName:"+realName;
//	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
}


结果

s1:com.lkt.entity.Student@362f0d54
s2:com.lkt.entity.Student@4b142196
false


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值