Spring beans life cycle management

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

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

[img]http://dl.iteye.com/upload/attachment/0073/1827/6b4807d9-deb8-3b2b-9e63-1f384cae6e66.jpg[/img]


[b]SimpleBeanWithInterface.java[/b]

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;
}
}
}




[b]initInterface.xml[/b]

<?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

[b]由程序结果可以看出,当分别使用annotation(@PostConstruct),实现接口InitializingBean,init-method方式来处理bean的初始化处理时, @PostConstruct方法最先执行,接口(afterPropertiesSet)方式其二执行,最后是init-method方式执行。[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值