Spring Bean容器对象如何初始化和销毁
Spring容器对象BeanFactory
//相对路径
XmlBeanFactory-->Resouce--->ClassPathResouce
Resource resouce = new ClassPathResource("applicationContext.xml");
BeanFactory beanFactory = new XmlBeanFactory(resouce);
Bean bean = (Bean)beanFactory.getBean("bean");
bean.show();
//绝对路径
XmlBeanFactory-->Resouce-FileSytemResouce
Resource resouce = new FileSystemResource("d:\\applicationContext.xml");
BeanFactory beanFactory = new XmlBeanFactory(resouce);
Bean bean =(Bean)beanFactory.getBean("bean");
bean.show();
ApplicationContext
ClassPathXmlApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean)ac.getBean("bean");
bean.show();
FileSystemXmlApplicationContext
ApplicationContext ac = new FileSystemXmlApplicationContext("d:\\applicationContext.xml");
Bean bean =(Bean)ac.getBean("bean");
bean.show();
Spring提供classpath访问器 可以直接访问
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
Bean bean =(Bean)ac.getBean("bean");
bean.show();
FileSystemXmlApplicationContext支持国际化,功能更强大
pring容器bean对象生命周期
(1)bean创建
(a) lazy-init="false"==lazy-init="default"
创建spring容器对象,就创建spring容器里bean对象
(b)lazy-init="true"
调用getBean spring容器才去创建bean实例
注意:timer定时器不能使用lazy-init="true"
(a)、(b)设置在<bean lazy-init="true"></bean>
设置在<beans default-lazy-init="true"></beans>
default-lazy-init="true"设置一个全局的对象创建延时
Spring Bean对象模式
spring默认采用singleton模式
singleton:(struts1)
单态:每次只产生一个实例
prototype:(struts2)
原型:可以产生多个对象
web框架使用:
scope="session": 一次会话
session:多次请求与响应叫会话
scope="request: 一次请求
Spring Bean初始化和销毁
初始化:init-method="init"bean对象init方法名
lazy-init="true"也延时了init-method
destroy-method="destroy" bean对象destroy方法名
完成资源释放
destroy-method必须是singleton模式一起使用
页:
[1]