• 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. trap命令使用

    分享一个shell脚本技巧,大家写shell脚本的时候,一般而言仅仅保证功能可用,但程序的鲁棒性却不是太好,不够健壮,多数是脚本处理 一些中断信号导致,应对非预期的系统信号,其实系统自带的trap命令 ...

  2. android NDK开发环境搭建

    android NDK开发环境搭建 2012-05-14 00:13:58 分类: 嵌入式 基于 Android NDK 的学习之旅-----环境搭建 工欲善其事必先利其器 , 下面介绍下 Eclip ...

  3. iTween基础之iTweenPath(自定义路径移动)

    在游戏开发中经常会用到让一个游戏对象按照指定的路线移动,iTweenPath就提供了可视化的编辑路径功能. iTweenPath 下载地址: http://download.csdn.net/deta ...

  4. cocos2dx 2.0+ 版本,IOS6.0+设置横屏

    使用cocos2dx 自带的xcode模板,是不能正常的设置为横屏的. 一共修改了三个地方: 在项目属性中:Deployment Info中,勾选上 Landscape left,以及 Landsca ...

  5. Js作用域与作用域链详解[转]

     一直对Js的作用域有点迷糊,今天偶然读到JavaScript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作 ...

  6. Android -- 获取汉字的首字母

    转换                                                                                             获取一个汉 ...

  7. Mac下的常用Shell命令

    今天介绍一下在Mac的终端中一些常用的Shell命令: 1.查看当前工作目录的完整路径 pwd (pwd的原意是:print work directiory,而不是密码password的意思,呵呵) ...

  8. navicat for mysql 注册码

    PremiumSoft Navicat for MySQL Enterprise Edition v8.0.27 姓名(Name):3ddown.com 组织(Organization):3ddown ...

  9. NYOJ-21 三个水杯 AC 分类: NYOJ 2014-02-08 11:35 174人阅读 评论(0) 收藏

    人生中第一个AC的广搜题目,喵呜,C++的STL果真不错, #include<stdio.h> #include<queue> #include<string.h> ...

  10. JS通过ajax动态读取xml文件内容

    http://www.sharejs.com/codes/javascript/8178 HTML文件代码如下 <!DOCTYPE html> <html> <head& ...