• bean的生命周期

为什么总是一个生命当做一个重点?

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("释放各种资源"); } }

Spring框架学习之第6节的更多相关文章

  1. Spring框架学习之第2节

    传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...

  2. Spring框架学习之第1节

    spring快速入门 ①   spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...

  3. Spring框架学习之第9节

    aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...

  4. Spring框架学习之第8节

    <bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...

  5. Spring框架学习之第3节

    model层(业务层+dao层+持久层) spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦 举例: 现在我们体验一下spring的di配合接口编程,完成一个字母大小写转换的案例 ...

  6. Spring框架学习之第7节

    配置Bean的细节 ☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大 ②如何给集合类型注入值 Java中主要的map,set,list / 数组 ...

  7. Spring框架学习之第5节

    request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...

  8. Spring框架学习之第4节

    从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...

  9. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

随机推荐

  1. Android实现页面跳转、ListView及其事件

    Android实现页面跳转.ListView及其事件 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 进入主页面后,使用ListView实现特 ...

  2. disable_irq()与disable_irq_nosync()区别

    disable_irq关闭中断并等待中断处理完后返回, 而disable_irq_nosync立即返回.

  3. Function-两个日期大小比较

    function checkDate(from,to){ if (from == "" || to == "") return 2; var rValue = ...

  4. OC学习心得【适合初学者】

    一.类和对象 1.OC语言是C语言的扩充,并且OC是iOS和OS X操作系统的编程语言. ①具备完善的面向对象特性: 封装:将现实世界中存在的某个客体的属性与行为绑定在一起,并放置在一个逻辑单元内 继 ...

  5. Notes of the scrum meeting(11/3)

    meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪                  ...

  6. 13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)

    今天花费了一天的时间来解决这个bug. 这种在程序运行期间出现的问题比较棘手,如果再没有规律的话就更难解决. 还好这个bug是由规律的,也就是说在程序执行半个小时左右后就会因为此异常而导致程序退出:那 ...

  7. Java Synchronized Blocks vs. Methods

    It's possible to synchronize both an entire method and a section of code within a method, and you ma ...

  8. [转]如何写出高效能TSQL -深入浅出SQL Server Relational Engine (含 SQL 2014 in-memory Engine)

    [转]如何写出高效能TSQL -深入浅出SQL Server Relational Engine (含 SQL 2014 in-memory Engine) http://blogs.technet. ...

  9. HDU 5792 World is Exploding 树状数组+枚举

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...

  10. Android开发-环境搭建以及HelloWorld

    最近开始进行Android的开发,没有基础完全从0开始.   首先,知道Android开发的官方网站: http://developer.android.com/index.html 网站本身教程非常 ...