一、Bean 生命周期

Spring bean的生命周期很容易理解。当bean被实例化时,可能需要执行一些初始化以使其进入可用状态。类似地,当不再需要bean并从容器中移除bean时,可能需要进行一些清理。

虽然,在bean实例化及其销毁之间存在幕后发生的活动列表,但本章仅讨论两个重要的bean生命周期回调方法,这些方法在bean初始化及其销毁时是必需的。

要定义bean的设置和拆卸,我们只需使用initmethod和/或destroy-method参数声明<bean> 。init-method属性指定在实例化时立即在bean上调用的方法。类似地,destroy-method指定在从容器中删除bean之前调用的方法。

示例如下:

(1)编写HelloWord.java

package com.tutorialspoint;

public class HelloWorld {
private String message; public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy() {
System.out.println("Bean will destroy now.");
}
}

(2)编写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"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" init-method = "init"
destroy-method = "destroy">
<property name = "message" value = "Hello World!"/>
</bean> </beans>

(3)编写MainApp.java

package com.tutorialspoint;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}

二、Bean Post 处理器

BeanPostProcessor接口定义,你可以实现提供自己的实例化逻辑,依赖解析逻辑等,您还可以实现一些自定义的逻辑Spring容器完成实例化,配置,并通过在一个或多个堵塞初始化Bean后回调方法BeanPostProcessor实现。

您可以配置多个BeanPostProcessor接口,并且您可以通过设置订单属性来控制这些BeanPostProcessor接口的执行顺序,前提是BeanPostProcessor实现了Ordered接口。

BeanPostProcessors在bean(或对象)实例上运行,这意味着Spring IoC容器实例化一个bean实例,然后BeanPostProcessor接口完成它们的工作。

一个ApplicationContext的自动检测与该执行中定义的任何Bean的BeanPostProcessor接口,并注册这些Bean类如后处理器,被通过在容器创建bean的适当调用。

示例如下:

(1)编写HelloWorld.java

package com.tutorialspoint;
public class HelloWorld {
private String message; public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy(){
System.out.println("Bean will destroy now.");
}
}

(2)编写InitHelloWorld.java

package com.tutorialspoint;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException; public class InitHelloWorld implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException { System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException { System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}

(3)编写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"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"
init-method = "init" destroy-method = "destroy">
<property name = "message" value = "Hello World!"/>
</bean> <bean class = "com.tutorialspoint.InitHelloWorld" /> </beans>

(4)编写MainApp.java

package com.tutorialspoint;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}

(5)运行MainApp.java中的main方法测试

结果如图:

Spring(四)之Bean生命周期、BeanPost处理的更多相关文章

  1. spring(二、bean生命周期、用到的设计模式、常用注解)

    spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...

  2. Spring事务,Bean生命周期

    一.事务相关: 1.Spring事务基于Spring AOP切面编程: 2.AOP基于代理模式,得到需要开启事务的代码的代理对象: 3.而没有开启事务的Service方法里调用了开启事务 @Trans ...

  3. spring容器对bean生命周期的管理三中方式

    spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)方式一:使用springXML配置中的init-meth ...

  4. spring中的bean生命周期

    1.实例化(在堆空间中申请空间,对象的属性值一般是默认值.通过调用createBeanInstance()方法进行反射.先获取反射对对象class,然后获取默认无参构造器,创建对象) 2.初始化(就是 ...

  5. Spring源码-Bean生命周期总览

  6. Spring中Bean生命周期

    Spring中的bean生命周期是一个重要的点,只有理解Bean的生命周期,在开发中会对你理解代码是非常有用的.对于Bean的周期,个人认为可以分为四个阶段.第一阶段:Bean的实例化,在该阶段主要是 ...

  7. spring源码阅读笔记10:bean生命周期

    前面的文章主要集中在分析Spring IOC容器部分的原理,这部分的核心逻辑是和bean创建及管理相关,对于单例bean的管理,从创建好到缓存起来再到销毁,其是有一个完整的生命周期,并且Spring也 ...

  8. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

  9. 金三银四,还在为spring源码发愁吗?bean生命周期,看了这篇就够了

    第一,这绝对是一个面试高频题. 比第一还重要的第二,这绝对是一个让人爱恨交加的面试题.为什么这么说?我觉得可以从三个方面来说: 先说会不会.看过源码的人,这个不难:没看过源码的人,无论是学.硬背.还是 ...

随机推荐

  1. bnu 10783 格斗游戏 线段与圆的关系

    格斗游戏 Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer IO format: %lld      Java class name: ...

  2. K:求取两个数的最大公约数的两个算法

    相关介绍:  最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为gcd(a,b).同样的,a,b,c的最大公约数记为gcd(a,b,c),多个整数的最 ...

  3. C# 调用C++DLL 类型转换

    内容转自网上····这里做 备份··· 原文链接: http://blog.csdn.net/miss_easy/article/details/52470964 /C++中的DLL函数原型为 //e ...

  4. 说说JSON和JSONP区别

    前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...

  5. HTML标签类型

    标签分类: 一.块标签:块标签是指本身属性为display:block;的元素. 1.默认占一行可以设置宽高, 2.在不设置宽度的情况下,块级元素的宽度是它父级元素内容的宽度 3.在不设置高度的情况下 ...

  6. c++实现全密码生成

    这里所谓的“全密码”指的是指定字符串中所有可能出现的密码.以字符串“0123456789”为例,可能出现的2位密码会有100个,即L^N个.(L代表字符串的长度,N代表要生成密码的位数). 第一种方法 ...

  7. 国内外有名的java论坛

     国内: www.chinajavaworld.com-论坛人很多,高手也多,不过好像都在潜水      www.cn-java.com -也很不错,文章很好,但是就是商业性浓了点.      www ...

  8. CountDownTimer的用法及原理

    1.主线程中使用 值得注意的是,CountDownTimer可以在主线程中直接使用.验证一下回调的执行线程.在主线程中执行如下代码 CountDownTimer countDownTimer = , ...

  9. Scratch-Blockly配置过程

    原文地址:https://blog.csdn.net/litianquan/article/details/82735876 Scratch-Blockly配置过程 由于Blockly案例库开发项目需 ...

  10. APP中的 H5和原生页面如何分辨?

    一.APP内嵌H5和原生的区别 1.原生的页面运行速度快,比较流畅.H5页面相对原生的运行性能低,特别是一些动画效果有明显卡顿. 2.H5页面的很多交互都没有原生的好,比如弹层.输入时候的页面滑动 等 ...