`
morgan117
  • 浏览: 4175 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Spring Bean的生命周期

阅读更多
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>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics