Spring Bean的生命周期

本文详细介绍了Spring框架中Beans的生命周期,包括实例化、属性设置、初始化及销毁等关键步骤。特别关注了Singleton与Prototype作用域的区别,以及实现特定接口或使用注解来定制这些过程的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Beans的生命周期:
1. 实例化:
默认为调用该Bean的无参构造。 如果该Bean存在父类,则首先调用父类的无参构造。
2. 设置属性值:
3. 如果Bean实现了BeanNameAware接口,调用setBeanName方法。
4. 如果Bean实现了BeanFactoryAware接口,调用setBeanFactory方法。
5. 如果Bean实现了InitializingBean接口,调用afterPropertiesSet方法。
6. 如果配置文件(beans.xml)里设置了init-Method方法(对应的annotation为PostConstruct), 将会执行配置的init-Method方法
7. 如果Bean的scope为singleton(默认),容器将会把该bean放入Spring Ioc的缓存池里,并将Bean的引用返回给调用者,Spring容器将继续管理这个bean,如果bean实现了DisposableBean,将会调用destroy方法。如果配置文件定义了destroy-method,将会调用该方法。
8. 如果Bean的scope为prototype, 容器将Bean返回给调用者,以后都将由调用者负责Bean的生命周期。spring不再管理。 prototype和singleton的区别在于:singleton的bean的上述周期都是在容器初始化的时候执行的。 而prototype是在getBean的时候执行的。

ps: AbstractApplicationContext 含有销毁容易的方法。。registerShutdownHook


package inter;

public class Phone {
protected int price;
protected String name;

public Phone(int price,String name){
System.out.println("parent param construct");
this.price = price;
this.name = name;

}

public Phone(){
System.out.println("parent default construct");

}


public void speak(){
System.out.println(name+" speaks");
}

public int getPrice() {

return price;
}

public void setPrice(int price) {
System.out.println("parent setPrice");
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("parent setName");
this.name = name;
}


}



package Impl;

import inter.Phone;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class IPhone extends Phone implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean{

public IPhone(int price, String name) {
super(price, name);
System.out.println("param construct");

}

public IPhone() {
System.out.println("default construct");
}

@PostConstruct
public void initMethod(){
System.out.println("init-Method");
}

@Override
public void speak(){
System.out.println(this.getName()+" general speaks ");

}

@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("setBeanFactory");

}

@Override
public void setBeanName(String arg0) {
System.out.println("bean name:"+arg0);

}


@Override
public void setPrice(int price) {
// TODO Auto-generated method stub
this.price = price;
System.out.println("son Set price");
}

@Override
public void setName(String name) {
this.name = name;
System.out.println("son Set name");
}

@Override
public void destroy() throws Exception {
System.out.println("destory");

}

public void testDestroy(){
System.out.println("destroy-method");

}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");

}






}




<?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-2.0.xsd">

<bean id="iphone" name="iphone2" class="Impl.IPhone" p:name="iphone4s" p:price="100" scope="singleton" destroy-method="testDestroy"/>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值