Spring Bean的生命周期例子
以下例子源于:W3Cschool,在此作记录

HelloWorld.java
package com.how2java.w3cschool.beanlife;
public class HelloWorld {
private String message;
public void getMessage() {
System.out.println("Your message:" + message);
}
public void setMessage(String message) {
this.message = message;
}
public void init() {
System.out.println("Bean is going through init");
}
public void destroy() {
System.out.println("Bean will destory now");
}
}
MainApp.java
package com.how2java.w3cschool.beanlife; 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("beanlife.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); //getBean是用来获取applicationContext.xml文件里bean的,括号内写的是对应的bean的id
obj.getMessage();
context.registerShutdownHook(); //需要注册一个在 AbstractApplicationContext 类中声明的关闭 hook 的 registerShutdownHook() 方法。它将确保正常关闭,并且调用相关的 destroy 方法。
}
}
beanlife.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id = "helloWorld"
class = "com.how2java.w3cschool.beanlife.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value ="Hello World!"></property>
</bean>
<!-- init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。
并且,上面指定bean对应的类为 HelloWorld,因此会到该bean(类)下去找对应的方法并调用--> </beans>
运行结果如下:

如果具有太多相同名称的初始化或者销毁方法的 Bean,那么并不需要在每一个 bean 上声明初始化方法和销毁方法。框架使用元素中的 default-init-method 和 default-destroy-method 属性提供了灵活地配置这种情况,即,在beans的配置中加上default-init-method 和 default-destroy-method 属性即可,无须在每个bean中都写一次对应的inti和destroy方法。
Spring Bean的生命周期例子的更多相关文章
- Spring Bean的生命周期(非常详细)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- spring bean的生命周期
掌握好spring bean的生命周期,对spring的扩展大有帮助. spring bean的生命周期(推荐看) spring bean的生命周期
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
- Spring Bean的生命周期相关博客
最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...
- Spring学习手札(四)谈谈Spring Bean的生命周期及作用域
在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...
- Spring Bean的生命周期详解(转)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring动态代理及Spring Bean的生命周期
数组添加值 public class DiTest { /** * 数组 */ private String [] arrays; /** * List:集合 */ private List<I ...
- Spring(三)--Spring bean的生命周期
Spring bean的生命周期 ApplicationContext Bean生命周期流程 1.需要的实体类 ackage com.xdf.bean; import org.springframew ...
- 第37讲 谈谈Spring Bean的生命周期和作用域
在企业应用软件开发中,Java 是毫无争议的主流语言,开放的 Java EE 规范和强大的开源框架功不可没,其中 Spring 毫无疑问已经成为企业软件开发的事实标准之一.今天这一讲,我将补充 Spr ...
随机推荐
- Google's Machine Learning Crash Course #04# First Steps with TensorFlow
1.使用 TensorFlow 的建议 Which API(s) should you use? You should use the highest level of abstraction tha ...
- Linux三剑客grep、sed、awk
grep grep file grep -i file grep -v file
- Golang利用select实现超时机制
所谓超时,比如上网浏览一些安全的网站,如果几分钟之后不做操作,那么就会让你重新登录.就所谓有时候出现goroutine阻塞的情况,那么我们如何避免整个程序进入阻塞情况,这时候就可以用select来设置 ...
- css盒子模型、边框border、外边距margin、填充padding、轮廓outline
盒子模型:盒子默认的宽度为容器的宽度,也可以自省设定宽度,高度根据内容适应,也可以自行设定高度.min-height设定最小高度 一个盒子包括外边距.边框.内边距和实际内容 Margin(外边距):清 ...
- Python 用pygame模块播放MP3
安装pygame(这个是python3,32位的) pip安装这个whl文件 装完就直接跑代码啦,很短的 import time import pygame file=r'C:\Users\chan\ ...
- Bootstrap3基础 img-thumbnail 给图片加一个圆角的边框
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- grub基本应用
一.基本概念 GRUB(boot loader): GRand Unified Bootloader 两个版本: grub .x: grup legacy grub .x: grub2 grub ...
- 【系列教程1】Gradle入门系列一:简介
Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注. 这篇文章是Gradle教程的第一篇,我们有两个目 ...
- 【Dalston】【第四章】容错保护(Hystrix)
我们在实践微服务架构时,通常会将业务拆分成一个个微服务,微服务之间通过网络进行通信,进行互相调用,造成了微服务之间存在依赖关系.我们知道由于网络原因或者自身的原因,服务并不能保证服务的100%可用,如 ...
- 题解—— 洛谷 p1269 信号放大器(贪心)
深刻的教训,不要写错读入 #include <cstdio> #include <algorithm> using namespace std; ; ; ,u[MAXM],v[ ...