在这篇文章中我们会了解一下spring中bean的lifecycle,并且在例子中实现自定义init-method和destroy-method。
bean的生命周期
在spring中,bean的lifecyle大体如下所示
自定义init-method和destroy-method
package com.liumiao.demo.spring;
public class Student implements Person {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("Student: set method: SetName:"+name);
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
System.out.println("Student: set method: SetCountry:"+country);
this.country = country;
}
private TeachingService teachingService;
public Student(){
System.out.println("Student Default construct is called...");
}
public void initMethod(){
System.out.println("User customized init-method is called");
}
public void destroyMethod(){
System.out.println("User customized destroy-method is called");
}
public void setTeachingService(TeachingService service){
teachingService=service;
}
@Override
public String sayhello(){
return "Hello, I am a student.";
}
@Override
public String provideTeachingService(){
return "I teach how to study...";
}
}
修改spring设定文件,设定自定义的函数
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:com/liumiao/demo/spring/Person.properties" />
<bean id="thePerson" class="com.liumiao.demo.spring.Student" init-method="initMethod" destroy-method="destroyMethod">
<property name="teachingService" ref="theService"></property>
<property name="name" value="${person.name}" />
<property name="country" value="${person.country}" />
</bean>
<bean id="theService" class="com.liumiao.demo.spring.SwimmingTeachingService">
</bean>
</beans>
执行结果
重执行结果中可以大体看到spring机制运行时候是如何加载自定义的函数的
十一月 27, 2016 8:51:27 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sun Nov 27 08:51:27 CST 2016]; root of context hierarchy
十一月 27, 2016 8:51:27 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/liumiao/demo/spring/spring-cfg.xml]
十一月 27, 2016 8:51:28 上午 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [com/liumiao/demo/spring/Person.properties]
Student Default construct is called...
Student: set method: SetName:liumiao.cn
Student: set method: SetCountry:ChinaPRC
User customized init-method is called
十一月 27, 2016 8:51:28 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sun Nov 27 08:51:27 CST 2016]; root of context hierarchy
Hello, I am a student.
I teach how to study...
ChinaPRC
liumiao.cn
User customized destroy-method is called

本文详细介绍了Spring框架中Bean的生命周期,包括初始化和销毁的过程。通过一个具体的示例演示了如何自定义初始化(init-method)和销毁(destroy-method)方法,并展示了这些方法在Spring上下文中是如何被调用的。
8730

被折叠的 条评论
为什么被折叠?



