spring bean的创建与消亡由spring容器进行管理,除了使用<bean><property/></bean>进行简单的属性配置之外,spring支持更人性化的方法

  • @PostConstruct @PreDestroy
  • xml的init-method和destroy-method
  • 实现InitializingBean和DisposableBean接口
 public class BeanInitMethod implements InitializingBean, DisposableBean {

     private int step;

     public int getStep() {
return step;
} public void setStep(int step) {
this.step = step;
} @PostConstruct
public void postConstruct() {
System.out.println("initialized by annotation");
step = 1;
} @PreDestroy
public void predestroy() {
System.out.println("destroyed by annotation");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("initialized by interface");
step = 2;
} @Override
public void destroy() throws Exception {
System.out.println("destroyed by interface");
} public void initFunc() {
System.out.println("initialized by xml");
step = 3;
} public void destroyFunc() {
System.out.println("destroyed by xml");
} public static void main(String[] argv) {
BeanInitMethod beanInitMethod;
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
beanInitMethod = (BeanInitMethod) beanFactory.getBean("initTestBean"); System.out.println("step=" + beanInitMethod.getStep()); System.out.println("program is done");
} }

对于“注解方式”还需要开启注解的支持,在上下文xml配置文件加入

<context:annotation-config/>

对于xml配置方式,则需要加入

<bean id="initTestBean" class="edu.xhyzjiji.cn.spring.BeanInitMethod" init-method="initFunc" destroy-method="destroyFunc"/>

当bean实例被创建时,会依据以下顺序执行初始化

initialized by annotation
initialized by interface
initialized by xml
step=3
program is done

spring bean初始化和销毁的更多相关文章

  1. spring bean初始化及销毁你必须要掌握的回调方法

    spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...

  2. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

  3. Spring Bean初始化之后执行指定方法

    转: Spring Bean初始化之后执行指定方法 2017年07月31日 15:59:33 vircens 阅读数:24807   Spring Bean初始化之后执行指定方法 在运用Spring进 ...

  4. bean初始化和销毁的几种方式

    Bean生命周期 Bean创建 -->初始化 -->销毁 1.自定义Bean初始化 和销毁的方法 init-method和destroy-method 创建Bike类 public cla ...

  5. Spring实现初始化和销毁bean之前进行的操作,三种方式

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  6. 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势

    文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...

  7. spring bean初始化顺序

    转载:http://blog.csdn.net/heyutao007/article/details/50326793 常用的设定方式有以下三种:通过实现 InitializingBean/Dispo ...

  8. 一次Spring Bean初始化顺序问题排查记录

    最近在使用Springboot的时候需要通过静态的方法获取到Spring容器托管的bean对象,参照一些博文里写的,新建了个类,并实现ApplicationContextAware接口.代码大致如下: ...

  9. Spring bean初始化以及管理

    在完成bean实例化后,spring会根据配置文件的设定情况对bean 的属性进行初始化, 1.autowire方式 (可查找自动装配对象 但bean中要有相应属性的set方法)这是一个自动装配的机制 ...

随机推荐

  1. java.lang.ClassNotFoundException错误原因汇总

    开发java很长时间了,还经常会遇到java.lang.ClassNotFoundException这样的错误,最近又处理了一次,起初怀疑是jdk版本比class文件的编译版本低了导致了,但是运维人员 ...

  2. Java 8 新特性终极版

    声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:Java 8 特性 – 终极手册,我还是坚持自己 ...

  3. poj 3692 Kindergarten (最大独立集之逆匹配)

    Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and al ...

  4. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  5. JavaScript 中的正常任务与微任务

    正常情况下,JavaScript的任务是同步执行的,即执行完前一个任务,然后执行后一个任务.只有遇到异步任务的情况下,执行顺序才会改变. 这时,需要区分两种任务:正常任务(task)与微任务(micr ...

  6. jsp filter登录限制过滤器

    http://www.cnblogs.com/hemingwang0902/archive/2012/01/09/2316956.html UserFilter.java package filter ...

  7. AngularJs练习Demo11引入Jquery

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  8. ios开发必备第三方库

    引言 作为iOS开发人员,在开发App的过程中怎么会不使用第三方库呢?相信没有App是不使用第三方库的! 网络库 网络库,这是开发必备,除非你的App玩单机.现在特别火也特别好用的网络库就数AFNet ...

  9. SQL Server类型与C#类型对应关系

    SQL类型 C#类型 bit bool tinyint byte smallint short int int bigint long real float float double money de ...

  10. Cocos2d-x 3.x部署到安卓

    一.前期准备 下载下列软件: Python2.7 (https://www.python.org/downloads/) Cocos2d-x 3.x (http://www.cocos2d-x.org ...