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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring 中,我们可以通过定义 Bean 来管理我们的 Java 对象。Bean 是由 Spring 容器来创建、组装和管理的。 在容器启动时,Spring 会读取配置文件(如 XML、JavaConfig 等)中的 Bean 定义,并通过反射机制创建 Bean 对象,然后将这些 Bean 对象放入容器中进行管理。 在创建 Bean 对象时,Spring 提供了多种初始化方式: 1. 实现 InitializingBean 接口,重写 afterPropertiesSet() 方法,该方法会在 Bean 实例化后、属性注入完成后被调用。 2. 在配置文件中使用 init-method 属性,该属性指定一个自定义的初始化方法,Spring 容器会在 Bean 实例化后、属性注入完成后调用该方法。 3. 使用 @PostConstruct 注解标注一个自定义的初始化方法,该方法会在 Bean 实例化后、属性注入完成后调用。 4. 在配置文件中使用工厂方法创建 Bean,该方法返回的对象就是要创建的 Bean,同时可以指定一个自定义的初始化方法,Spring 容器会在 Bean 实例化后、属性注入完成后调用该方法。 以上方法都可以用来实现 Bean 的初始化操作。需要注意的是,在使用以上方法时,Bean 的属性已经被注入,所以可以在初始化方法中使用 Bean 的属性。 同时需要注意的是,如果 Bean 实现了 DisposableBean 接口或者配置了 destroy-method 属性或者使用 @PreDestroy 注解标注的销毁方法,Spring 容器会在容器关闭时调用销毁方法,进行资源的释放等操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值