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. Oracle Replace()函数与translate()函数 替换函数

    简要比较: replace 字符串级别的代替 如:SELECT REPLACE('accd','cd','ef') from dual; --> aefd translate 字符级别的代替 如 ...

  2. Linux 下查看内存使用情况方法总结

    Linux查看CPU和内存使用情况:http://www.cnblogs.com/xd502djj/archive/2011/03/01/1968041.html 在做Linux系统优化的时候,物理内 ...

  3. YAML_17 Playbook 综合

    Playbook1.语法特性如下:(1)"---"首行顶格开始(2)#号注释(3)缩进统一,不同的缩进代表不同的级别,缩进要对齐,空格和tab不能混用(4)区别大小写,键值对k/v ...

  4. $\text{fhq-treap}$总结

    \(\text{fhq-treap}\)总结 又名范浩强\(\text{treap}\),是一种无旋\(\text{treap}\).其原理同\(\text{treap}\)一样都是通过维护一个随机堆 ...

  5. Cogs 876. 游戏(DP)

    游戏 ★ 输入文件:game1.in 输出文件:game1.out 简单对比 时间限制:1 s 内存限制:128 MB USACO/game1 A Game游戏 译 by 肖遥 描述 有如下一个双人游 ...

  6. 常用命令备忘 xargs

    xargs 作为使用率很高的命令,但是长久不用就会模糊了记忆,所以要记录下来. 获取所有的cobbler相关的布尔值然后全部设置为真 getsebool -a|grep cobbler|awk '{p ...

  7. [内网渗透]IPC$共享连接

    0x01 简介 IPC$(Internet Process Connection)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道,可以通过验证用户名和密码获得相应的 ...

  8. Mysql中EXISTS关键字用法、总结

    在做教务系统的时候,一个学生(alumni_info)有多个教育经历(alumni_education),使用的数据库是mysql,之前使用左链接查询的,发现数据量才只有几万条时,查询就很慢了,早上想 ...

  9. Nginx+Tomcat多实例及负载均衡配置

    Nginx+Tomcat多实例及负载均衡配置 采用nginx的反向代理负载均衡功能,配合后端的tomcat多实例来实现tomcat WEB服务的负载均衡 01 安装nginx服务 安装所需的pcre库 ...

  10. 【Vue.js游戏机实战】- Vue.js实现老虎-机抽奖总结

    大家好!先上图看看本次案例的整体效果. 完整版实战课程附源码:[Vue.js游戏机实战]- Vue.js实现老虎-机抽奖 实现思路: Vue component实现老虎-机组件,可以嵌套到任意要使用的 ...