Spring bean的几个属性:scopeinit-methoddestroy-methoddepends-on等。

Scope

在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围。

scope分类:singleton, prototype, request, session, global session。

这里的singleton和设计模式里面的单例模式不一样,标记为singleton的bean是由容器来保证这种类型的bean在同一个容器内只存在一个共享实例,而单例模式则是保证在同一个Classloader中只存在一个这种类型的实例。

init-method

是指创建bean时调用的方法,注意,不是创建bean的方法。

destroy-method

是指销毁bean时调用的方法,同样,不是销毁bean的方法。

注意:scope为prototype的bean,容器会将创建好的对象实例返回给请求方,之后,容器就不再拥有其引用,请求方需要自己负责当前对象后继生命周期的管理工作,包括该对象的销毁。

所以:scope为singleton的bean的destroy方法则是在容器关闭时执行,而scope为prototype的bean是不会执行destroy方法的。

depends-on

用于指定bean初始化及销毁时的顺序。注意上面的结论

<bean id="helloApi" class="cn.javass.spring.chapter2.helloworld.HelloImpl"/>
<bean id="decorator"
class="cn.javass.spring.chapter3.bean.HelloApiDecorator"
depends-on
="helloApi">
<property name="helloApi"><ref bean="helloApi"/></property>
</bean>

“decorator”指定了“depends-on”属性为“helloApi”,所以在“decorator”Bean初始化之前要先初始化“helloApi”而在销毁“helloApi”之前先要销毁“decorator”,大家注意一下销毁顺序。

Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:

通过实现 InitializingBean / DisposableBean 接口;
    通过<bean> 元素的 init-method / destroy-method属性;
    通过@PostConstruct或@PreDestroy注解。

Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method

Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method

参考:

Spring bean 的init-method和destroy-method
Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
Spring scope属性详解

----------------------------------------------------------

可以和@Component一起使用的注解:

@Lazy(true) -- 延迟初始化

@DependsOn({"managedBean"}) --  初始化及销毁时的顺序

@Qualifier -- 见 Spring 依赖注入(DI)的注解

@Primary -- 当有多个候选时,被注解的bean作为首选项,否则异常。

 

Spring bean的初始化及销毁的更多相关文章

  1. Spring3实战第二章第一小节 Spring bean的初始化和销毁三种方式及优先级

    Spring bean的初始化和销毁有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 优先级第二通过 <bean& ...

  2. Spring bean 实现初始化、销毁方法的方式及顺序

    Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...

  3. spring bean的初始化以及销毁

    spring bean初始化或销毁时执行某些方法,有很多使用场景.比如初始化时,启动bean中的线程池.销毁时释放资源,个人比较喜欢实现InitializingBean和 DisposableBean ...

  4. Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)

    一.Bean的初始化和销毁 在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持.在使用Java配置和注解配置下提供如下两种方式: ...

  5. Spring学习笔记--初始化和销毁Bean

    可以使用bean的init-method和destroy-method属性来初始化和销毁bean.定义一个Hero类: package com.moonlit.myspring; public cla ...

  6. spring boot之 Bean的初始化和销毁(4)

    原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...

  7. 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式

    bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...

  8. 四、Srping之Bean的初始化和销毁

    Srping之Bean的初始化和销毁方法 通常,bean的初始化和销毁方法我们有三个地方可以入手,分别是: 自定义初始化,销毁方法 实现spring提供的InitializingBean(初始化逻辑) ...

  9. 12、生命周期-@Bean指定初始化和销毁方法

    12.生命周期-@Bean指定初始化和销毁方法 Bean的生命周期:创建->初始化->销毁 容器管理bean的生命周期 我们可以自定义初始方法和销毁方法,容器在bean进行到当期那生命周期 ...

随机推荐

  1. Spark与Pandas中DataFrame对比

      Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...

  2. php 数组与数组 的交集和差集

    注意,必须是第一个参数的数组的长度比较长才可以使用 $a1=array("0","1","2","3"); $a2=ar ...

  3. 微信开发,对象转换为xml时候引用XStream这个类报错处理方案

    报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved. /**  * 扩展XStream 支持CDATA  */ privat ...

  4. Js控制弹窗实现在任意分辨率下居中显示

    弹窗居中比较烦人的是怎么才能在任意分辨率下实现居中显示.1,html部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  5. modelsim常见错误

    1. Error: (vlog-7) Failed to open design unit file "D:/Xilinx/verilog/src/glbl.v" in read ...

  6. CocoaPods did not set the base configuration of your project 问题解决方式

    今天在使用pod install的时候.出现了 [!] CocoaPods did not set the base configuration of your project because you ...

  7. java concurrency

    Dealing with InterruptedExceptionhttp://www.ibm.com/developerworks/java/library/j-jtp05236/index.htm ...

  8. JAVA文件转换为Base64

    JAVA文件转换为Base64 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...

  9. Android AlarmManager的一些问题

    我开始的代码是这样写的 alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender); 我的本意是设定五秒后启动闹钟 但是每次都是我设置完闹钟 ...

  10. linux没有那个文件或目录

    linux脚本执行 报错 没有那个文件或目录 但是ls 看一下,明明有文件 原因: vim filename然后用命令 :set ff可看到dos或unix的字样,如果的确是dos格式的, 那么用se ...