欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring的Bean生命周期方法

程序员文章站 2024-02-11 20:32:04
...

 

Spring的Bean生命周期方法

 代码实现:

PersonService.java

package cn.itcast.beanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{
	private String name;

	public PersonService() {
		super();
		System.out.println("构造函数被调用");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		System.out.println("setName函数被调用");
		this.name = name;
	}

	@Override
	public void setBeanName(String arg0) {
		System.out.println("BeanNameAware接口  " + arg0);
		
	}

	@Override
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("BeanFactoryAware接口 " + arg0);
		
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		System.out.println("ApplicationContextAware接口 " + arg0);
		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("InitializingBean接口 "+"afterPropertiesSet方法");
		
	}	
	
	public void init(){
		System.out.println("init-method='init' 方法被調用");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("DisposableBean接口 "+"destroy方法");
		
	}
	
	public void myDestroy(){
		System.out.println("destroy-method "+"destroy方法");
	}
		
}

MyBeanPostProcessor.java

package cn.itcast.beanlife;

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

public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
		System.out.println("BeanPostProcessor接口 "+"postProcessAfterInitialization");
		return arg0;
		
	}

	@Override
	public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
		System.out.println("BeanPostProcessor接口 "+"postProcessBeforeInitialization");
		return arg0;
		
	}
}

applicationContext.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"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="personService" init-method="init" destroy-method="myDestroy" class="cn.itcast.beanlife.PersonService">
		<property name="name">
			<value>wangjiang</value>
		</property>	
	</bean>
	
	<bean id="myBeanPostProcessor" class="cn.itcast.beanlife.MyBeanPostProcessor"/>
		
</beans>

Test.java:

package cn.itcast.beanlife;

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

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/beanlife/applicationContext.xml");
		PersonService ps = (PersonService) ac.getBean("personService");
		System.out.println(ps.getName());
		
	}
}

输出结果:

Spring的Bean生命周期方法

注意:

      如果使用BeanFactory来创建bean对象,会缺少ApplicationContext以及BeanPostProcessor的before和after方法。输出结果如下所示:

Spring的Bean生命周期方法

 

 

 

相关标签: Bean生命周期