一、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. Shiro官方快速入门10min例子源码解析框架2-Session

    Shiro自身维护了一套session管理组件,它可以独立使用,并不单纯依赖WEB/Servlet/EJB容器等环境,使得它的session可以任何应用中使用. 2-Session)主要介绍在quic ...

  2. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  3. 第二章.JSP/Servlet及相关技术详解

    JSP的4种基本语法: 1.JSP注释: <%-- JSP注释部分 --%> 2.JSP声明: <%! //声明一个整型变量 public int count; //声明一个方法 p ...

  4. 浅谈ul布局以及table布局

    我个人对于某些言论说要注重html语义化在布局中的应用,我反而不怎么感冒,试试兼容IE7&&项目期相对较赶的情况下,我还是推荐快速开发为主,兼容性强为主. 如果布局中需要用户边框,推荐 ...

  5. grunt-contrib-watch 监控 JS 文件改变来运行预定义的Tasks

    依赖于 GruntJs ~0.4.0 监控 JS 文件改变来运行预定义的Tasks Demo: watch: { scripts: { files: ['src/**/*.js'], tasks: [ ...

  6. 在 Ubuntu上使用 MySQL

    MySQL 安装配置 https://help.ubuntu.com/12.04/serverguide/mysql.html MySQL Manual http://dev.mysql.com/do ...

  7. 新版qq canvas 动态背景

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. C语言四舍五入算法

    对h进行四舍五入 1. 网络上搜索来的: C语言取整规则: (int)(h + 0.5) 2. 二级教程: 四舍五入并精确到小数点后面的第n位: 实例:

  9. method invocation

    package method.invocation; public class MethodInvocation { public static void main(String[] args) { ...

  10. volley4--RequestQueue

    源码: /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, V ...