6.1 Initialization和Destruction

  • spring对bean初始化的时候和销毁时候进行某些操作提供了支持

    • 利用@Bean的initMethoddestroyMethod(和xml配置的init-method和destory-method相同)
    • 利用JSR-250的@PostConstruct@PreDestroy

6.2 示例

6.2.1 @Bean形式的Initialization和Destruction

6.2.1.1 新建服务java类

package com.wisely.prepost;

public class BeanWayService {
public void init(){
System.out.println("init-method-bean");
}
public BeanWayService() {
super();
System.out.println("初始化构造函数-bean");
}
public void destroy(){
System.out.println("destory-method-bean");
} }

6.2.1.2 新建配置java类

package com.wisely.prepost;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanWayConfig {
@Bean(initMethod="init",destroyMethod="destroy")
public BeanWayService beanWayService(){
return new BeanWayService();
}
}

测试

package com.wisely.prepost;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.prepost");
context.close(); } }

输出结果

初始化构造函数-bean
init-method-bean
destory-method-bean

6.2.2 JSR-250形式的Initialization和Destruction

6.2.2.1 添加jsr250-api到maven依赖

添加如下到pom.xml

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>

6.2.2.2 添加jsr250形式的服务类

package com.wisely.prepost;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.stereotype.Service; @Service
public class JSR250WayService {
@PostConstruct
public void init(){
System.out.println("init-method-annotation");
}
public JSR250WayService() {
super();
System.out.println("初始化构造函数-annotation");
}
@PreDestroy
public void destroy(){
System.out.println("destory-method-annotation");
}
}

6.2.2.3 测试

package com.wisely.prepost;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wisely.prepost");
context.close(); } }

输出结果

初始化构造函数-annotation
init-method-annotation
destory-method-annotation

06点睛Spring4.1-Bean的初始化和销毁的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. Spring bean的初始化及销毁

    Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...

  10. @Bean 指定初始化和销毁方法

    bean 的生命周期 bean 的创建 --> 初始化 --> 销毁 ioc 容器管理 bean 的声明周期 可以自定义初始化和销毁方法 构造器( 对象创建 )被调用时机 单实例:在容器启 ...

随机推荐

  1. MySQL 是怎么保证数据一致性的(转载)

    在<写数据库同时发mq消息事务一致性的一种解决方案>一文的方案中把分布式事务巧妙转成了数据库事务.我们都知道关系型数据库事务能保证数据一致性,那数据库到底是怎么设计事务这一特性的呢? 一. ...

  2. 多路选择器,加法器原理及verilog实现

    1.数据选择器是指经过选择,把多个通道的数据传到唯一的公共数据通道上.实现数据选择功能的逻辑电路称为数据选择器,它的作用相当于多个输入的单刀多掷开关.本例程以四选一数据选择器(电平触发)为例. 四选一 ...

  3. Vijos 1057 盖房子

    二次联通门 : Vijos 1057 盖房子 /* Vijos 1057 盖房子 简单的dp 当前点(i, j)所能构成的最大的正方形的边长 为点(i - 1, j - 1)与(i, j - 1), ...

  4. Iptables之一基本应用

    Firewall :防火墙,隔离工具 工作于主机或网络的边缘,对于进出本主机或网络的报文根据事先定义好的检查规则作匹配检测,对于能够被规则所匹配到的报文做出相应处理的组件 主机防火墙 网络防火墙 ID ...

  5. python format 时间格式

    trainData['survey_time'] = pd.to_datetime(trainData['survey_time'],format = '%Y/%m/%d %H:%M') trainD ...

  6. 值得H5前端学习的60个JS插件(含DEMO演示)

    下面也可以说是H5前端学习的js插件大全.基本包含了大部分的前端最前沿的js插件和库. 布局 SuperEmbed.js- 是一个Javascript库,可检测出网页上的内嵌视频并使他们能够变成响应式 ...

  7. JavaScript data types and data structures

    JavaScript data types and data structures Programming languages all have built-in data structures, b ...

  8. AAR文件简介

    假如我们希望提供一个带有资源文件的第三方库给别人使用,总不能直接把源代码给别人,但是我们知道eclipse打包的时候不能包含res的资源文件,于是Android在发布Android studio的时候 ...

  9. shell获取今天、明天、昨天、n天、周、月、年日期

    1.获取今天日期 $ date -d now +%Y-%m-%d   或者$ date +%F 1    2 2.获取明天日期 $ date -d next-day +%Y-%m-%d$ date - ...

  10. Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas

    Arthas 用户文档 — Arthas 3.1.0 文档https://alibaba.github.io/arthas/ alibaba/arthas: Alibaba Java Diagnost ...