spring bean 的生命周期

 

###一、Bean的一生过程
####先来看以下的图(Bean的一生)
 
这里写图片描述
 

####可以简述为以下九步

实例化bean对象(通过构造方法或者工厂方法)
设置对象属性(setter等)(依赖注入)
如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。(和下面的一条均属于检查Aware接口)
如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身
将Bean实例传递给Bean的前置处理器的postProcessBeforeInitialization(Object bean, String beanname)方法
调用Bean的初始化方法
将Bean实例传递给Bean的后置处理器的postProcessAfterInitialization(Object bean, String beanname)方法
使用Bean
容器关闭之前,调用Bean的销毁方法

####先看一个最简单的一生(没有使用Bean的后置处理器)

package com.linjie.cycle;

import org.springframework.beans.factory.BeanNameAware;

/**
 * @author LinJie
 * @Description:一个学生类(Bean),能体现其生命周期的Bean
 */
public class Student implements BeanNameAware {
 private String name;

 //无参构造方法
 public Student() {
  super();
 }

 /** 设置对象属性
  * @param name the name to set
  */
 public void setName(String name) {
  System.out.println("设置对象属性setName()..");
  this.name = name;
 }
 
 //Bean的初始化方法
 public void initStudent() {
  System.out.println("Student这个Bean:初始化");
 }
 
 //Bean的销毁方法
 public void destroyStudent() {
  System.out.println("Student这个Bean:销毁");
 }
 
 //Bean的使用
 public void play() {
  System.out.println("Student这个Bean:使用");
 }

 /* 重写toString
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
  return "Student [name = " + name + "]";
 }

 //调用BeanNameAware的setBeanName()
 //传递Bean的ID。
 @Override
 public void setBeanName(String name) {
  System.out.println("调用BeanNameAware的setBeanName()..." );
 }
 
}
1

package com.linjie.cycle;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试类
 * @author LinJie
 *
 */
public class CycleTest {
    @Test
  public void test() {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  Student student = (Student) context.getBean("student");
  //Bean的使用
  student.play();
  System.out.println(student);
  //关闭容器
  ((AbstractApplicationContext) context).close();
 }
}
 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
   <!-- init-method:指定初始化的方法
        destroy-method:指定销毁的方法 -->
  <bean id="student" class="com.linjie.cycle.Student" init-method="initStudent" destroy-method="destroyStudent">
  <property name="name" value="LINJIE"></property>
  </bean>
</beans>
1
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
   <!-- init-method:指定初始化的方法
        destroy-method:指定销毁的方法 -->
  <bean id="student" class="com.linjie.cycle.Student" init-method="initStudent" destroy-method="destroyStudent">
  <property name="name" value="LINJIE"></property>
  </bean>
</beans>
1
---------------------
作者:浅然_
来源:CSDN
原文:https://blog.csdn.net/w_linux/article/details/80086950
版权声明:本文为博主原创文章,转载请附上博文链接!

 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值