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