bean的作用范围
* 它是可以通过配置的方式,来调整作用范围。
* 配置的属性时:bean标签的scope属性
* 属性的取值:
* singleleton:单例的(默认值)
* prototype:多例的(当spring接管struts2的action创建时,action必须配置此值)
* request:作用范围是一次请求和当前请求的转达
* session:作用范围是一次会话
* globalsession:作用范围是一次全局会话。
,默认不配置scope属性时
@SuppressWarnings("all")
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.获取容器
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
//2.根据容器id获取对象
//CustormerService cs = (CustormerService) factory.getBean("staticcustormerservice");
CustormerService cs1 = (CustormerService) factory.getBean("custormerService");
CustormerService cs2 = (CustormerService) factory.getBean("custormerService");
//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
System.out.println(cs1 == cs2);
打印true
配置scope=prototype;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
<!-- <bean id="custormerService" class="cn.pro.service.impl.CustormerServiceimpl"></bean> -->
<bean id="custormerService" class="cn.pro.service.impl.CustormerServiceimpl" scope="prototype"></bean>
<bean id="icustormerDao" class="cn.pro.dao.impl.ICustormerDaoimpl"></bean>
</beans>
打印false
bean的生命周期。
* bean的生命周期
* 涉及bean标签的两个属性
* init-method 初始化方法和销毁方法
* destroy-method
* 单例:
* 出生:容器创建,对象就出生了
* 活着:只要容器在,对象就一直存在
* 死亡:容器销毁,对象消亡
* 多例:
* 出生:每次使用时创建对象
* 活着:只要对象在使用中就一直活着
* 死亡:当对象长时间不使用,并且也没有别的对象引用时,由java的垃圾回收器回收。
单例模式:
cn.pro.service.impl.CustormerServiceimpl
package cn.pro.service.impl;
import cn.pro.dao.ICustormerDao;
import cn.pro.dao.impl.ICustormerDaoimpl;
import cn.pro.service.CustormerService;
//客户的业务层实现层
public class CustormerServiceimpl implements CustormerService{
//ICustormerDao custormerdao = new ICustormerDaoimpl();
//ICustormerDao custormerdao = BeanFactory.getICustormerDao();
//private ICustormerDao custormerdao = null;
public CustormerServiceimpl(){
System.out.println("对象创建了");
}
public void init(){
System.out.println("对象初始化");
}
public void destroy(){
System.out.println("对象销毁");
}
public void saveCustormer() {
// TODO Auto-generated method stub
System.out.println("custormer saved!");
//custormerdao.SaveCustormer();
}
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
<!-- <bean id="custormerService" class="cn.pro.service.impl.CustormerServiceimpl"></bean> -->
<bean id="custormerService" class="cn.pro.service.impl.CustormerServiceimpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>
<bean id="icustormerDao" class="cn.pro.dao.impl.ICustormerDaoimpl"></bean>
</beans>
调用
// TODO Auto-generated method stub
//1.获取容器
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据容器id获取对象
CustormerService cs = (CustormerService) ac.getBean("custormerService");
ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
cs.saveCustormer();
ac.close();
对象创建了
对象初始化
custormer saved!
对象销毁
ClassPathXmlApplicationContext的父类applicationcontext没有close方法。
将上面例子中的bean.xml中的scope改成多例prototype
再运行,观察不到销毁destroy方法。