关闭

spring bean初始化

73人阅读 评论(0) 收藏 举报
分类:
package com.provider.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

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;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Hello implements InitializingBean,DisposableBean,BeanNameAware,BeanFactoryAware{
	private String name;
	
	public void setName(String name) {
		System.out.println("执行Hello: setName");
		this.name = name;
	}
	

	public Hello(){  
        System.out.println("执行Hello: 构造方法");  
    }  
      
    @PostConstruct  
    public void postConstruct() {    
       System.out.println("执行Hello: postConstruct");    
    }    
      
    
      
    public void initMethod() {  
        System.out.println("执行Hello: init-method");  
    }  
  
    @PreDestroy  
    public void preDestroy()  {  
        System.out.println("执行Hello: preDestroy");  
    }  
      
    public void destroy() throws Exception {  
        System.out.println("执行Hello: destroy");  
    }  
      
    public void destroyMethod() {  
        System.out.println("执行Hello: destroy-method");  
    }

    public void afterPropertiesSet() throws Exception {  
        System.out.println("执行Hello: afterPropertiesSet");   
    }  
    
  
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("执行Hello: setBeanFactory"); 
	}

	public void setBeanName(String name) {
		System.out.println("执行Hello: setBeanName"); 
	}

	  
    public static void main(String[] args) {
    	ClassPathXmlApplicationContext  applicationContext = new ClassPathXmlApplicationContext("spring-core.xml"); 
    	System.out.println(applicationContext.containsBean("hello"));
    	applicationContext.close();
	}

	

}


	 <bean id="hello" class="com.provider.bean.Hello" init-method="initMethod" destroy-method="destroyMethod">
	 <property name="name" value="ppp"></property>
	 </bean>

执行Hello: 构造方法
执行Hello: setName
执行Hello: setBeanName
执行Hello: setBeanFactory
执行Hello: afterPropertiesSet
执行Hello: init-method
true
执行Hello: destroy
执行Hello: destroy-method

过程:

默认构造方法

set方法注入属性

BeanNameAware  setBeanName

BeanFactoryAware setBeanFactory

BeanPostProcessor   postProcessBeforeInitialization

InitializingBean  afterPropertiesSet

init-method

BeanPostProcessor postProcessAfterInitialization

DisposableBean destroy

destroy-method

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11699次
    • 积分:663
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:6篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论