ry的专栏

Stay hungry, Stay foolish

spring bean初始化

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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013038630/article/details/51556195
个人分类: spring
上一篇java8 ConcurrentHashMap
下一篇java Callable Future
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭