• 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. Java基础学习总结--Java对象的序列化和反序列化

    一.序列化和反序列化的概念 把对象转换成字节序列的过程称之为对象的序列化 把字节序列恢复为对象的过程称之为对象的反序列化 对象序列化的主要用途: 1)把对象的字节序列永久的保存到硬盘上,通常放在一个文 ...

  2. linux 负载 待读

    相关文章: 理解 Linux 的处理器负载均值(翻译)

  3. 单点登录的原理与CAS技术的研究

    1.什么是单点登录? 关于单点登录技术的说明参考文章:http://www.cnblogs.com/yupeng/archive/2012/05/24/2517317.html 一般来说,整个原理大家 ...

  4. JS 学习笔记--2--变量的声明

      1.ECMAScript 中规定所有的关键字.保留字.函数名.函数名.操作符等都是区分大小写的. 2.标识符:指变量.函数.属性的名字:标识符组成:以字母.下划线.$ 开头,其他字母可以含有数字, ...

  5. poi实现Excel比较

    http://stackoverflow.com/questions/866346/easiest-way-to-compare-two-excel-files-in-java http://stac ...

  6. Oracle 11g安装与使用

    作为一个新手,学习Oracle,就连安装oracle都感觉到吃力! 经过不间断的搜罗.学习.尝试,找到一些比较有用的“指导”,罗列如下: 1. http://www.2cto.com/database ...

  7. Orchard中文学习视频录制完成

    Orchard学习视频已登录百度传课: http://www.chuanke.com/3027295-124882.html http://pan.baidu.com/s/13zc0u 1.orcha ...

  8. JAVA 对象数组,加载图片实例 分类: Java Game 2014-08-14 16:57 80人阅读 评论(0) 收藏

    主函数: package com.mywork; import java.awt.Color; import java.awt.Image; import javax.swing.ImageIcon; ...

  9. .NET设计模式(6):原型模式(Prototype Pattern)(转)

    概述 在软件系统中,有时候面临的产品类是动态变化的,而且这个产品类具有一定的等级结构.这时如果用工厂模式,则与产品类等级结构平行的工厂方法类也要随着这种变化而变化,显然不大合适.那么如何封装这种动态的 ...

  10. ios frame、bound和center定义及使用场景总结

    frame:指的是视图在父视图的坐标系统中的大小和位置. bound:指的是视图在视图本身的坐标系统中的大小(位置起点是原点). center:指的是视图在父视图坐标系统中的中心点. frame和bo ...