开发环境: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方式执行。
- 大小: 76 KB
分享到:
相关推荐
spring beans jar包,需要另外3个包。加上log4j和logging
赠送jar包:spring-beans-5.2.0.RELEASE.jar; 赠送原API文档:spring-beans-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-beans-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.2.0....
赠送jar包:spring-beans-5.0.10.RELEASE.jar; 赠送原API文档:spring-beans-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-beans-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.0.10....
org.springframework.beans-2.5.5.A.jar, org.springframework.beans-2.5.6.A.jar, org.springframework.beans-2.5.6.SEC01.jar, org.springframework.beans-3.0.0.M1.jar, org.springframework.beans-3.0.0.M2.jar,...
赠送jar包:spring-beans-5.2.0.RELEASE.jar; 赠送原API文档:spring-beans-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-beans-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.2.0....
开发工具 spring-beans-4.3.6.RELEASE开发工具 spring-beans-4.3.6.RELEASE开发工具 spring-beans-4.3.6.RELEASE开发工具 spring-beans-4.3.6.RELEASE开发工具 spring-beans-4.3.6.RELEASE开发工具 spring-beans-...
《Spring Beans 模块详解与版本差异分析》 Spring框架是Java开发中广泛使用的轻量级框架,其中Spring Beans模块是其核心组成部分,它负责管理对象的生命周期和依赖注入。本篇文章将深入探讨Spring Beans的功能特性...
spring-beans-5.1.0.RELEASE,最新spring-bean jar包。
spring-beans-4.3.xsd spring-beans-4.3.xsd spring-beans-4.3.xsd spring-beans-4.3.xsd
《Spring Beans DTD详解——基于C#的编程视角》 在软件开发领域,Spring框架以其卓越的灵活性和可扩展性,成为了Java应用开发的事实标准。然而,Spring不仅仅局限于Java,其设计理念和机制同样对其他语言如C#有着...
spring beans dtd
赠送jar包:spring-beans-5.1.3.RELEASE.jar; 赠送原API文档:spring-beans-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-beans-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.1.3....
spring-beans-5.0.2.jar为官网spring最新jar包,可以放心使用。
《Spring框架中的beans配置文件详解——以spring-beans-3.0.xsd和3.1.xsd为例》 在Spring框架中,`spring-beans`是核心组件之一,它负责管理对象的生命周期和依赖关系。`spring-beans`的配置文件通常以`.xsd`为后缀...
赠送jar包:spring-beans-5.3.7.jar; 赠送原API文档:spring-beans-5.3.7-javadoc.jar; 赠送源代码:spring-beans-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.3.7.pom; 包含翻译后的API文档:...
赠送jar包:spring-beans-4.3.12.RELEASE.jar; 赠送原API文档:spring-beans-4.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-beans-4.3.12.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-beans-4.3.12....
赠送jar包:spring-beans-5.2.7.RELEASE.jar; 赠送原API文档:spring-beans-5.2.7.RELEASE-javadoc.jar; 赠送源代码:spring-beans-5.2.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.2.7....
赠送jar包:spring-beans-5.3.10.jar; 赠送原API文档:spring-beans-5.3.10-javadoc.jar; 赠送源代码:spring-beans-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-beans-5.3.10.pom; 包含翻译后的API文档...