bean的上下文和bean工厂以及生命周期
2016-09-10 12:45
302 查看
u 从ApplicationContex 应用上下文容器中获取bean和从bean工厂容器中获取bean
具体案例:
//从ApplicationContext中取bean
ApplicationContextac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");
//当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是 singleton)从bean中取出student
//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么
//容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
BeanFactoryfactory = new XmlBeanFactory(
newClassPathResource("com/hsp/ioc/beans.xml"));
factory.getBean("student");
结论:
1.如果使用ApplicationContext ,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
2.如果是 BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)
3.规定: 一般没有特殊要求,应当使用ApplicatioContext完成(90%)
bean 的 scope的细节
入门案例:
//获取两个student
Students1=(Student) ac.getBean("student");
Students2=(Student) ac.getBean("student");
System.out.println(s1+""+s2);
l request
l session
l global-session
是在web开发中才有意义.
u 三种获取ApplicationContext 对象引用的方法
1. ClassPathXmlApplicationContext-> 通过类路径
2. FileSystemXmlApplicationContext-> 通过文件路径
举例:
ApplicationContext ac=newFileSystemXmlApplicationContext("文件路径beans.xml / applicationContext.xml");
3. XmlWebApplicationContext
u bean的生命周期
为什么总是一个生命周期当做一个重点?
Servlet -> servlet生命周期 init() destory()
java对象生命周期.
往往笔试,面试总喜欢问生命周期的问题
① 实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singleton)实例化到内存
② 调用set方法设置属性
③ 如果你实现了bean名字关注接口(BeanNameAware)则,可以通过setBeanName获取id号
④ 如果你实现了 bean工厂关注接口,(BeanFactoryAware),则可以获取BeanFactory
⑤ 如果你实现了 ApplicationContextAware接口,则调用方法
//该方法传递ApplicationContext
publicvoid setApplicationContext(ApplicationContext arg0)
throwsBeansException {
//TODO Auto-generated method stub
System.out.println("setApplicationContext"+arg0);
}
⑥ 如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessBeforeInitialization方法
⑦ 如果你实现InitializingBean 接口,则会调用 afterPropertiesSet
⑧ 如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法.
⑨ 如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessAfterInitialization方法
⑩ 使用我们的bean
11. 容器关闭
12. 可以通过实现DisposableBean 接口来调用方法 destory
13. 可以在<bean destory-method=”fun1”/> 调用定制的销毁方法
小结: 我们实际开发中往往,没有用的这么的过程,常见的是:
1->2->6->10->9->11
上机练习: 把使用每个 bean的时间记录到一个recoder.txt文件 ,内容是
xxbean. 使用时间是 : 1999-11-11 11:11:11
问题:通过BeanFactory来获取bean对象,bean的生命周期是否和Applicationcontext 是一样吗?
不是一样的,bean是工厂中创建的生命周期会简单一些:
尽量使用 scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大.
具体案例:
//从ApplicationContext中取bean
ApplicationContextac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");
//当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是 singleton)从bean中取出student
//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么
//容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
BeanFactoryfactory = new XmlBeanFactory(
newClassPathResource("com/hsp/ioc/beans.xml"));
factory.getBean("student");
结论:
1.如果使用ApplicationContext ,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
2.如果是 BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)
3.规定: 一般没有特殊要求,应当使用ApplicatioContext完成(90%)
bean 的 scope的细节
入门案例:
//获取两个student
Students1=(Student) ac.getBean("student");
Students2=(Student) ac.getBean("student");
System.out.println(s1+""+s2);
l request
l session
l global-session
是在web开发中才有意义.
u 三种获取ApplicationContext 对象引用的方法
1. ClassPathXmlApplicationContext-> 通过类路径
2. FileSystemXmlApplicationContext-> 通过文件路径
举例:
ApplicationContext ac=newFileSystemXmlApplicationContext("文件路径beans.xml / applicationContext.xml");
3. XmlWebApplicationContext
u bean的生命周期
为什么总是一个生命周期当做一个重点?
Servlet -> servlet生命周期 init() destory()
java对象生命周期.
往往笔试,面试总喜欢问生命周期的问题
① 实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singleton)实例化到内存
② 调用set方法设置属性
③ 如果你实现了bean名字关注接口(BeanNameAware)则,可以通过setBeanName获取id号
④ 如果你实现了 bean工厂关注接口,(BeanFactoryAware),则可以获取BeanFactory
⑤ 如果你实现了 ApplicationContextAware接口,则调用方法
//该方法传递ApplicationContext
publicvoid setApplicationContext(ApplicationContext arg0)
throwsBeansException {
//TODO Auto-generated method stub
System.out.println("setApplicationContext"+arg0);
}
⑥ 如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessBeforeInitialization方法
⑦ 如果你实现InitializingBean 接口,则会调用 afterPropertiesSet
⑧ 如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法.
⑨ 如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessAfterInitialization方法
⑩ 使用我们的bean
11. 容器关闭
12. 可以通过实现DisposableBean 接口来调用方法 destory
13. 可以在<bean destory-method=”fun1”/> 调用定制的销毁方法
小结: 我们实际开发中往往,没有用的这么的过程,常见的是:
1->2->6->10->9->11
上机练习: 把使用每个 bean的时间记录到一个recoder.txt文件 ,内容是
xxbean. 使用时间是 : 1999-11-11 11:11:11
问题:通过BeanFactory来获取bean对象,bean的生命周期是否和Applicationcontext 是一样吗?
不是一样的,bean是工厂中创建的生命周期会简单一些:
尽量使用 scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大.
package com.beanlife; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean { private String name; private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } //如果是要调用有参数的构造函数,需要在beans.xml中配置factory-method public PersonService(String abc){ System.out.println("PersonService 函数"); } public PersonService(){ System.out.println("PersonService 函数"); } public void setName(String name) { System.out.println("setName(String name) 函数"); this.name = name; } public void sayHi(){ System.out.println("hi "+ name); } //该方法可以给arg0表示正在被实例化得bean id public void setBeanName(String arg0) { // TODO Auto-generated method stub System.out.println("setBeanName 被调用 值"+arg0); } //该方法可以传递beanFactroy public void setBeanFactory(BeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("setBeanFactory "+arg0); } //该方法传递ApplicationContext public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("setApplicationContext"+arg0); } //也可以通过注解的方式来配置哪个方法init-method //@PostConstruct public void init(){ System.out.println("我自己的init方法"); } // public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("afterPropertiesSet()"); } //定制我们的销毁方法 //@PreDestroy public void mydestory(){ System.out.println("释放各种资源"); } }
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="personService" destroy-method="mydestory" class="com.beanlife.PersonService"> <!-- 这里注入我们属性,前提就是有setName才能ok --> <property name="name"> <value>xiaoming</value> </property> </bean> <!-- 配置我们的自己后置处理器(有点类似我们的filter) --> <bean id="myBeanPostProcessor" class="com.beanlife.MyBeanPostProcessor" /> </beans>
package com.beanlife; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessAfterInitialization 函数被调用"); System.out.println(arg0+" 被创建的时间是"+new java.util.Date()); return arg0; } public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessBeforeInitialization 函数被调用"); return arg0; } }
package com.beanlife; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; public class App1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac=new ClassPathXmlApplicationContext("com/beanlife/beans.xml"); /* 通过bean工厂 BeanFactory factory = new XmlBeanFactory( new ClassPathResource("com/beanlife/beans.xml")); PersonService ps=(PersonService) factory.getBean("personService"); */ PersonService ps=(PersonService) ac.getBean("personService"); ps.sayHi(); } }
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; //也可以通过注解的方式来配置哪个方法init-method <span style="white-space:pre"> </span>@PostConstruct <span style="white-space:pre"> </span>public void init(){ <span style="white-space:pre"> </span>System.out.println("我自己的init方法"); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public void afterPropertiesSet() throws Exception { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>System.out.println("afterPropertiesSet()"); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>//定制我们的销毁方法 <span style="white-space:pre"> </span>@PreDestroy <span style="white-space:pre"> </span>public void mydestory(){ <span style="white-space:pre"> </span>System.out.println("释放各种资源"); <span style="white-space:pre"> </span>} 这是使用注入的,则不需要再xml中配置init-method
相关文章推荐
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- 详解Java的Spring框架中bean的定义以及生命周期
- BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
- 第三章 对象,对象工厂以及应用程序上下文
- 使用应用上下文容器(ApplicationContext)来获取bean的bean的生命周期
- BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
- Spring的Bean的生命周期以及Bean的后置处理器
- Spring学习之使用静态工厂方法以及实例工厂方法配置bean
- BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
- Spring -- spEL&Bean的生命周期&工厂方法配置Bean
- 通过上下文获取bean和通过bean工厂获取bean
- (3)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- (2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- 从ApplicationContex 应用上下文容器中获取bean和从bean工厂容器中获取bean的区别
- spring管理bean的生命周期与spring通过工厂方法配置bean
- Spring学习笔记一: Bean工厂(BeanFactory )与应用上下文(ApplicationContext)BeanFactory
- 学习Spring(六) -- Spring中Bean的作用域以及生命周期
- BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
- Spring学习笔记一: Bean工厂(BeanFactory )与应用上下文(ApplicationContext)BeanFactory
- Spring自带的几种应用上下文以及Bean的声明周期