详述Spring框架bean生命周期

11 篇文章 0 订阅

详述Spring框架bean生命周期

通过构造方法或工厂方法创建bean对象——>为bean属性赋值——>调用 bean 的初始化方法,即init-method指定方法——>bean实例化完毕,可以使用——>容器关闭, 调用 bean 的销毁方法,即destroy-method指定方法。

init-method

init-method:在设置bean的属性后执行的自定义初始化方法,注意:①、该方法不能有参数;②、对象每创建一次就会执行一次该方法;

destroy-method

destroy-method:该参数中的方法只有bean标签属性scope为singleton且关闭Spring IOC容器时才会被调用,注意:该方法不能有参数

package com.zzu.vo;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Student {
	
	private Date date;
	
	public Student() {
		super();
		System.out.println("构造方法");
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date name) {
		System.out.println("setter方法");
		this.date = name;
	}
	public void a() {
		System.out.println("初始化---init方法");
	}
	public void b() {
		System.out.println("destroy");
	}
}
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean  id="date" class="java.util.Date"></bean>	
	<bean  class="com.zzu.vo.Student" init-method="a" destroy-method="b" p:date-ref="date"></bean>	
</beans>

编写测试类,代码如下: 

public class Test {

	public static void main(String[] args) {
		
	ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");//创建集合(IOC容器),并将.xml文件中bean放在容器中 			
    	Student stu = applicationContext.getBean(Student.class);
    	System.out.println(stu.getDate()); 	
    	applicationContext.close();
	}
}

运行结果:

首先执行构造方法----通过构造方法或工厂方法创建bean对象——>然后执行set方法----为bean属性赋值——>调用 bean 的初始化方法----init-method指定方法——>bean实例化完毕,可以使用-----stu对象调用getDate方法——>容器关闭, 调用 bean 的销毁方法----destroy-method指定方法。

创建多个bean对象(默认单例模式下)

import com.zzu.vo.Student;

public class Test {

	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");//创建集合(IOC容器),并将spring.xml中bean放在容器中 	
    	        Student stu = applicationContext.getBean(Student.class);
    	        System.out.println(stu.getDate()); 
    	
    	        stu = applicationContext.getBean(Student.class);
    	        System.out.println(stu.getDate()); 
    	        applicationContext.close();

	}
}

 运行结果:

首先执行构造方法----通过构造方法或工厂方法创建bean对象——>然后执行set方法----为bean属性赋值——>调用 bean 的初始化方法----init-method指定方法——>bean实例化完毕,可以使用-----stu对象调用getDate方法(默认为单例模式下,因此多次调用getDate方法,每次从IoC容器获取的Java对象都是同一个)——>容器关闭, 调用 bean 的销毁方法----destroy-method指定方法。

 

当开启多例模式后,此时destroy方法不会执行:

运行结果为:

destroy方法只有bean标签属性scope为singleton且关闭Spring IOC容器时才会被调用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值