spring之Bean的生命周期详解

 更新时间:2017年05月17日 08:49:16   作者:阿木侠  
本篇文章主要介绍了spring之Bean的生命周期详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Bean的生命周期:

Bean的定义——Bean的初始化——Bean的使用——Bean的销毁

Bean的定义

Bean 是 spring 装配的组件模型,一切实体类都可以配置成一个 Bean ,进而就可以在任何其他的 Bean 中使用,一个 Bean 也可以不是指定的实体类,这就是抽象 Bean 。

Bean的初始化

Spring中bean的初始化回调有两种方法

一种是在配置文件中声明init-method="init",然后在一个实体类中用init()方法来初始化

另一种是实现InitializingBean接口,覆盖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-3.0.xsd"> 
 
  <bean id="init-one" class="org.spring.test.BeanInitDemo1" init-method="init"> 
    <property name="message" value="这里是配置文件中为message赋值"></property> 
  </bean> 
</beans> 

BeanInitDemo1类:

package org.spring.test; 
 
public class BeanInitDemo1 { 
  private String message; 
 
  public String getMessage() { 
    return message; 
  } 
 
  public void setMessage(String message) { 
    this.message = message; 
  } 
  public void init(){ 
    this.setMessage("这里是init()方法初始化设值"); 
  } 
} 

测试类:

package org.spring.test; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
 
public class Test { 
 
  public static void main(String[] args) { 
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    BeanInitDemo1 bid = (BeanInitDemo1) context.getBean("init-one"); 
    System.out.println(bid.getMessage()); 
  } 
 
} 

运行结果:

这里是init()方法初始化设值

原因:init()初始化方法的调用是在配置文件的Bean初始化之后执行的, 所以改变了配置文件中对message的赋值。

第二种:

配置文件:

<?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-3.0.xsd"> 
   
  <bean id="init-two" class="org.spring.test.BeanInitDemo2"> 
    <property name="message" value="这里是配置文件中为message赋值"></property> 
  </bean> 
</beans> 

编写BeanInitDemo2类,使其实现InitializingBean接口

package org.spring.test; 
 
import org.springframework.beans.factory.InitializingBean; 
 
public class BeanInitDemo2 implements InitializingBean{ 
  private String message; 
 
  public String getMessage() { 
    return message; 
  } 
 
  public void setMessage(String message) { 
    this.message = message; 
  } 
 
  public void afterPropertiesSet() throws Exception { 
    // TODO Auto-generated method stub 
    this.setMessage("这里覆盖了InitializingBean接口的afterPropertiesSet()方法设值"); 
  } 
   
} 

测试:

package org.spring.test; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
 
public class Test { 
 
  public static void main(String[] args) { 
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    BeanInitDemo2 bid = (BeanInitDemo2) context.getBean("init-two"); 
    System.out.println(bid.getMessage()); 
  } 
 
} 

运行结果: 这里覆盖了InitializingBean接口的afterPropertiesSet()方法设值

原因相同,afterPropertiesSet()方法在配置文件的Bean初始化后执行,所以改变了配置文件中对message的赋值

Bean的使用

Spring中有两种使用bean的方法:

1, BeanFactory:

BeanFactory factory= new XmlBeanFactory(new ClassPathResource("bean.xml"));
factory.getBean("student");

BeanFactory是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用getBean方法才会抛出异常,也就是说当使用BeanFactory实例化对象时,配置的bean不会马上被实例化。当你使用该bean时才会被实例化(getBean)。

2, ApplicationContext:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

如果使用ApplicationContext,则配置的bean如果是singleton不管你用还是不用,都被实例化。ApplicationContext在初始化自身时检验,这样有利于检查所依赖属性是否注入。ApplicationContext是BeanFactory的子类,除了具有BeanFactory的所有功能外还提供了更完整的框架功能,例如国际化,资源访问等。所以通常情况下我们选择使用ApplicationContext。

Bean的销毁

Bean的销毁和初始化一样,都是提供了两个方法

一是在配置文件中声明destroy-method="cleanup",然后在类中写一个cleanup()方法销毁

二是实现DisposableBean接口,覆盖destory()方法

第一种:

配置文件:

<?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-3.0.xsd"> 
 
  <bean id="destory-one" class="org.spring.test.BeanDestoryDemo1" destroy-method="cleanup"> 
    <property name="message" value="这里是配置文件中为message赋值"></property> 
  </bean> 
</beans> 

BeanDestoryDemo1类:

package org.spring.test; 
 
public class BeanDestoryDemo1 { 
  private String message; 
 
  public String getMessage() { 
    return message; 
  } 
 
  public void setMessage(String message) { 
    this.message = message; 
  } 
  public void cleanup(){ 
    System.out.println("销毁之前可以调用一些方法"); 
  } 
} 

测试:

package org.spring.test; 
 
import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
public class DestortTest { 
  public static void main(String[] args) { 
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    BeanDestoryDemo1 bdd = (BeanDestoryDemo1) context.getBean("destory-one"); 
    System.out.println(bdd.getMessage()); 
    context.registerShutdownHook(); 
  } 
} 

运行结果:

context.registerShutdownHook()是为spring注册关闭吊钩,程序退出之前关闭spring容器,如果没有

context.registerShutdownHook();将不会执行cleanup()方法。

第二种:

配置文件:

<?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-3.0.xsd"> 
 
  <bean id="destory-two" class="org.spring.test.BeanDestoryDemo2"> 
    <property name="message" value="这里是配置文件中为message赋值"></property> 
  </bean> 
</beans>  

BeanDestoryDemo2类:

package org.spring.test; 
 
import org.springframework.beans.factory.DisposableBean; 
 
public class BeanDestoryDemo2 implements DisposableBean{ 
  private String message; 
 
  public String getMessage() { 
    return message; 
  } 
 
  public void setMessage(String message) { 
    this.message = message; 
  } 
 
  public void destroy() throws Exception { 
    // TODO Auto-generated method stub 
    System.out.println("同样,销毁之前调用的方法"); 
  } 
} 

测试:

package org.spring.test; 
 
import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
public class DestortTest { 
  public static void main(String[] args) { 
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    BeanDestoryDemo2 bdd = (BeanDestoryDemo2) context.getBean("destory-two"); 
    System.out.println(bdd.getMessage()); 
    context.registerShutdownHook(); 
  } 
}  

运行结果:

Spring可以管理singleton作用域的Bean的生命周期,所以在Bean初始化及销毁之前可以做一些工作,更灵活的管理Bean。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring中@Primary注解的作用详解

    Spring中@Primary注解的作用详解

    这篇文章主要介绍了Spring中@Primary注解的作用详解,@Primary 注解是Spring框架中的一个注解,用于标识一个Bean作为默认的实现类,当存在多个实现类时,通过使用@Primary注解,可以指定其中一个作为默认的实现类,以便在注入时自动选择该实现类,需要的朋友可以参考下
    2023-10-10
  • JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析

    JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析

    这篇文章主要介绍了JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • MongoDB整合Spring实例详细讲解(含代码)

    MongoDB整合Spring实例详细讲解(含代码)

    这篇文章主要介绍了MongoDB整合Spring实例详细讲解(含代码),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • springcloud引入spring-cloud-starter-openfeign失败的解决

    springcloud引入spring-cloud-starter-openfeign失败的解决

    这篇文章主要介绍了springcloud 引入spring-cloud-starter-openfeign失败的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • SpringBoot实现性能翻倍的五种配置方案

    SpringBoot实现性能翻倍的五种配置方案

    在当今高并发的互联网环境下,微服务架构的性能优化成为了开发者必须面对的挑战,SpringBoot作为Java生态中最流行的微服务框架之一,其默认配置虽然能满足大多数场景需求,但在极端性能要求下往往显得力不从心,本文将揭示5个常被开发者忽略的SpringBoot配置项
    2026-01-01
  • 详解在Spring-Boot中实现通用Auth认证的几种方式

    详解在Spring-Boot中实现通用Auth认证的几种方式

    这篇文章主要介绍了详解在Spring-Boot中实现通用Auth认证的几种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Springboot @Transactional使用时需注意的几个问题记录

    Springboot @Transactional使用时需注意的几个问题记录

    本文详细介绍了Spring Boot中使用`@Transactional`注解进行事务管理的多个方面,包括事务的隔离级别(如REPEATABLE_READ)和传播行为(如REQUIRES_NEW),并指出了在同一个类中调用事务方法时可能遇到的问题以及解决方案,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • SpringCloud LoadBalancer自定义负载均衡器使用解析

    SpringCloud LoadBalancer自定义负载均衡器使用解析

    LoadBalancerClient 是 SpringCloud 提供的一种负载均衡客户端,Ribbon 负载均衡组件内部也是集成了 LoadBalancerClient 来实现负载均衡,本文给大家深入解析 LoadBalancerClient 接口源码,感兴趣的朋友跟随小编一起看看吧
    2023-04-04
  • 分布式调度XXL-Job整合Springboot2.X实战操作过程(推荐)

    分布式调度XXL-Job整合Springboot2.X实战操作过程(推荐)

    这篇文章主要介绍了分布式调度XXL-Job整合Springboot2.X实战操作,包括定时任务的使用场景和常见的定时任务,通过本文学习帮助大家该选择哪个分布式任务调度平台,对此文感兴趣的朋友一起看看吧
    2022-04-04
  • 基于springBoot配置文件properties和yml中数组的写法

    基于springBoot配置文件properties和yml中数组的写法

    这篇文章主要介绍了springBoot配置文件properties和yml中数组的写法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11

最新评论