标签:
为什么总是一个生命当做一个重点?
Servlet –> servlet生命周期
Java对象生命周期
往往笔试,面试总喜欢问生命周期的问题?
① 实例化(当我们的程序加载beans.xml文件时)把我们的bean(前提是单态的即scope=singleton)实例化到内存
② 调用set方法设置属性
③ 如果你实现了bean名字关注接口(BeanNameAware)则,可以通过setBeanName获取id号
④ 如果你实现了bean工厂关注接口,则(BeanFactoryAware)则可以获取bean工厂,beanFactory
⑤ 如果你实现了ApplicationContextAware接口,则调用方法
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("setApplicationContext"+arg0);
}
⑥ 如果bean和一个后置处理器关联了,则会自动去调用Object postProcessBeforeInitialization方法
⑦ 了InitializingBean接口,则会调用afterPropertiesSet方法
⑧ 如果自己在<bean init-method=”init”>则可以在bean中定义自己的初始化方法
⑨ 如果bean和一个后置处理器关联了,则会自动去调用Object postProcessAfterInitialization方法
⑩ 使用我们的bean
? 容器关闭
? 可以通过实现DisposableBean接口来调用方法destroy
? 可以在<bean destroy-method=”fun1” /> 调用定制的销毁方法
小结:
我们在实际开发中往往没有用到这么多的过程,常见的过程是
1--2--6-10-9-11
问题:通过BeanFactory来获取bean对象,bean的生命周期是否和Applicationcontext 是一样吗?
不是一样,bean在工厂中创建的生命周期会简单一些
具体
项目结构
beans.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" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-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="mydestroy" init-method="init" class="com.litao.beanlife.PersonService"> <!-- -这里注入我们属性,前提就是有setName才能OK --> <property name="name"> <value>xiaoming</value> </property> </bean> <bean id="personService2" class="com.litao.beanlife.PersonService"> <!-- -这里注入我们属性,前提就是有setName才能OK --> <property name="name"> <value>xiaohong</value> </property> </bean> <!-- 配置我们自己的后置处理器(有点类似我们的fiter) --> <bean id="myBeanPostProcessor" class="com.litao.beanlife.MyBeanPostProcessor" /> </beans>
App1.java
package com.litao.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/litao/beanlife/beans.xml"); //BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/litao/beanlife/beans.xml")); PersonService ps = (PersonService)ac.getBean("personService"); //PersonService ps = (PersonService)factory.getBean("personService"); ps.sayHi(); } }
MyBeanPostProcessor.java
package com.litao.beanlife; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { //arg0就是beans.xml中要被实例的bean对象 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; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessBeforeInitialization函数被调用"); return arg0; } }
PersonService.java
package com.litao.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.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean{ private String name; public PersonService(){ System.out.println("PersonService构造函数被调用"); } public PersonService(String abc){ System.out.println("PersonService构造函数被调用"); } public String getName() { return name; } 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); } //该方法可以传递beanFactory 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); } public void init(){ System.out.println("我自己的init方法"); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("afterPropertiesSet()"); } //定制我们的销毁方法 public void mydestroy() { // TODO Auto-generated method stub //我们可以关闭数据连接,socket,文件流,释放该bean的资源 System.out.println("释放各种资源"); } }
标签:
原文地址:http://www.cnblogs.com/litao0505/p/5777492.html