spring bean 生命周期

43 篇文章 0 订阅
18 篇文章 0 订阅

                                  spring bean 生命周期

spring bean的生命周期从容器启动开始到容器停止运行,如图

代码如下

package com.spring.model;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Person implements DisposableBean, InitializingBean {

	private String name;

	Person() {
		System.out.println("Constructor of person bean is invoked!");
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	//Bean initialization code
	public void afterPropertiesSet() throws Exception {
		System.out.println("Initializing method of person bean is invoked!");
	}

	
	//Bean destruction code
	public void destroy() throws Exception {
		System.out.println("Destroy method of person bean is invoked!");
	}

	public void customDestroy() throws Exception {
		System.out.println("Custom destroy method of person bean is invoked!");
	}

	public void customInit() throws Exception {
		System.out.println("Custom initializing method of person bean is invoked!");
	}
}
package com.spring.processer;

import org.springframework.beans.factory.config.BeanPostProcessor;

public class InstantiationTracingBeanPostProcessor implements  BeanPostProcessor{
	// simply return the instantiated bean as-is
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        System.out.println("Bean '" + beanName + "' before created : " + bean.toString());
        return bean; // we could potentially return any object reference here...
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("Bean '" + beanName + "' created : " + bean.toString());
        return bean;
    }
}
package com.spring.main;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.model.Person;

public class Demoapp {

	public static void main(String[] args) {

		// Reading configuration from the spring configuration file.
		ConfigurableApplicationContext   context = new ClassPathXmlApplicationContext("spring-config.xml");

		Person myperson = context.getBean("personBean", Person.class);

		System.out.println("Name= " + myperson.getName());

		// Closing the context object.
		context.close();
	}
}

spring 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.xsd">

	<!-- <bean name="personBean" class="com.spring.model.Person"> <property 
		name="name" value="Jason Clarke" /> </bean> -->
	<bean name="personBean" class="com.spring.model.Person"
		init-method="customInit" destroy-method="customDestroy">
		<property name="name" value="Jason Clarke" />
	</bean>

	<!-- when the above bean (messenger) is instantiated, this custom BeanPostProcessor 
		implementation will output the fact to the system console -->
	<bean class="com.spring.processer.InstantiationTracingBeanPostProcessor" />
</beans>

运行结果

Constructor of person bean is invoked!
Bean 'personBean' before created : com.spring.model.Person@604ed9f0
Initializing method of person bean is invoked!
Custom initializing method of person bean is invoked!
Bean 'personBean' created : com.spring.model.Person@604ed9f0
Name= Jason Clarke
Destroy method of person bean is invoked!
Custom destroy method of person bean is invoked!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值