Spring beans life cycle management

开发环境:SpringSource Tool Suite 2.9.2 RELEASE  JDK: 1.6
程序类别: Spring Template Project--->Simple Spring Utility Project

Spring bean的生命周期如下,以下程序实验图中红框内容:




SimpleBeanWithInterface.java
package net.codercn.prospring3.ch5;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class SimpleBeanWithInterface implements InitializingBean{
	private static final String DEFAULT_NAME = "这是默认名字";
	private String name = null;
	private int age = Integer.MIN_VALUE;
	
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@PostConstruct
	public void myInit() {
		System.out.println("annotation方式执行");
	}
	
	public void myInit2(){
		System.out.println("默认方法执行");
	}
	public void afterPropertiesSet() throws Exception {
		System.out.println("Initialzing bean");
		if(name == null){
			System.out.println("Using default name");
			name = DEFAULT_NAME;
		}
		if(age == Integer.MIN_VALUE){
			throw new IllegalArgumentException(
				"You must set the age property of any beans of type " + SimpleBeanWithInterface.class);
		}
	}
	public String toString(){
		return "Name: " + name + "\nAge " + age;
	}
	
	public static void main(String[] args){
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:/META-INF/spring/initInterface.xml");
		ctx.refresh();
		SimpleBeanWithInterface simpleBean1 = getBean("simpleBean1",ctx);
		SimpleBeanWithInterface simpleBean2 = getBean("simpleBean2",ctx);
		SimpleBeanWithInterface simpleBean3 = getBean("simpleBean3",ctx);
	}
	
	private static SimpleBeanWithInterface getBean(String beanName,
			ApplicationContext ctx){
		try{
			SimpleBeanWithInterface bean = (SimpleBeanWithInterface) ctx.getBean(beanName);
			System.out.println(bean);
			return bean;
		} catch(BeanCreationException ex){
			System.out.println("An error occured in bean configuration: "
				+ ex.getMessage());
			return null;
		}
	}
}




initInterface.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.1.xsd"
		default-lazy-init="true">
		

	
	<context:annotation-config/>
	<context:component-scan base-package="net.codercn.prospring3.ch5.SimpleBeanWithInterface" />
	
	
	<bean id="simpleBean1"
		class="net.codercn.prospring3.ch5.SimpleBeanWithInterface" init-method="myInit2">
		<property name="name">
			<value>David</value>
		</property>
		<property name="age">
			<value>10</value>
		</property>
	</bean>
	<bean id="simpleBean2"
		class="net.codercn.prospring3.ch5.SimpleBeanWithInterface" >
		<property name="age">
			<value>11</value>
		</property>
	</bean>
	<bean id="simpleBean3"
		class="net.codercn.prospring3.ch5.SimpleBeanWithInterface" >
		<property name="name">
			<value>David</value>
		</property>
	</bean>

</beans>



程序输出:

annotation方式执行
Initialzing bean
默认方法执行
Name: David
Age 10
annotation方式执行
Initialzing bean
Using default name
Name: 这是默认名字
Age 11
annotation方式执行
Initialzing bean
An error occured in bean configuration: Error creating bean with name 'simpleBean3' defined in class path resource [META-INF/spring/initInterface.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: You must set the age property of any beans of type class net.codercn.prospring3.ch5.SimpleBeanWithInterface

由程序结果可以看出,当分别使用annotation(@PostConstruct),实现接口InitializingBean,init-method方式来处理bean的初始化处理时, @PostConstruct方法最先执行,接口(afterPropertiesSet)方式其二执行,最后是init-method方式执行。

猜你喜欢

转载自coderanch.iteye.com/blog/1669628