关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和  destory-method方法

第三种是:通过bean实现InitializingBean和 DisposableBean接口

下面演示通过  @PostConstruct 和 @PreDestory

1:定义相关的实现类:

  1. package com.myapp.core.annotation.init;
  2. import javax.annotation.PostConstruct;
  3. import javax.annotation.PreDestroy;
  4. public class PersonService {
  5. private String  message;
  6. public String getMessage() {
  7. return message;
  8. }
  9. public void setMessage(String message) {
  10. this.message = message;
  11. }
  12. @PostConstruct
  13. public void  init(){
  14. System.out.println("I'm  init  method  using  @PostConstrut...."+message);
  15. }
  16. @PreDestroy
  17. public void  dostory(){
  18. System.out.println("I'm  destory method  using  @PreDestroy....."+message);
  19. }
  20. }

2:定义相关的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  9. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->
  10. <context:annotation-config />
  11. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
  12. <property name="message" value="123"></property>
  13. </bean>
  14. </beans>

其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;

测试类:

  1. package com.myapp.core.annotation.init;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest {
  5. public static void main(String[] args) {
  6. ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");
  7. PersonService   personService  =  (PersonService)context.getBean("personService");
  8. personService.dostory();
  9. }
  10. }

测试结果:

I'm  init  method  using  @PostConstrut....123
I'm  destory method  using  @PreDestroy.....123

其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

类来告诉Spring容器采用的 常用 注解配置的方式:

只需要修改配置文件为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  9. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->
  10. <!-- <context:annotation-config /> -->
  11. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  12. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
  13. <property name="message" value="123"></property>
  14. </bean>
  15. </beans>

同样可以得到以上测试的输出结果。

通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进的更多相关文章

  1. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  2. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  3. Spring实现初始化和销毁bean之前进行的操作,三种方式

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  4. 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式

    1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...

  5. Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean

    Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ...

  6. spring实战三装配bean之Bean的作用域以及初始化和销毁Bean

    1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...

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

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

  8. 三种不同实现初始化和销毁bean之前进行的操作的比较

    Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...

  9. 【spring源码学习】Spring @PostConstruct和@PreDestroy实例

    在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...

随机推荐

  1. web前端开发必懂之一:JS继承和继承基础总结

    首先,推荐一篇博客豪情的博客JS提高: http://www.cnblogs.com/jikey/p/3604459.html ,里面的链接全是精华, 一般人我不告诉他; 我们会先从JS的基本的设计模 ...

  2. hdu2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. eclipse-mvn打包跳过junit测试类

    修改pom.xml,在build选项加上plugins的这段如下: <build> ..... <plugins> <plugin> <groupId> ...

  4. java eclipse打jar包和执行jar中的main函数

    jar包使用eclipse打包步骤 右键需要打包的项目->选择Export 到这里有两种打包的方式 1.如果项目中没有使用其他第三方包等,则直接选择下图中的第一种即可(JAR file) 2.如 ...

  5. C# 公历转农历

    /// <summary>     /// LunDay 的摘要说明.     /// 用法说明     /// 直接调用即可,比较简单     /// </summary> ...

  6. MySQL的Sleep进程

    php的垃圾回收机制,其实只针对于php本身. 对于mysql,php没权利去自动去释放它的东西. 如果你在页面执行完毕前不调用mysql_close(),那么mysql那边是不会关闭这个连接的. 如 ...

  7. ARP协议工作流程

    地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上的所有主机, ...

  8. 卸载移动硬盘出现 device is busy

    umount /dev/sdb1 # device is busy fuser -m -v /dev/sdb1 # 查看 fuser -m -k /dev/sdb1 # 杀死进程

  9. Process

    --- - hosts: test gather_facts: false tasks: - name: Task1 shell: ls notify: Hander3 - name: Task2 s ...

  10. MATLAB for循环优化三例

    最近一周,对MATLAB有进行了新一轮的学习,对其矩阵化编程的思维有了更深入的了解.确实精妙! 例1: 将矩阵A= [1 2 3; 2 4 3; 3 4 5]中所有的数字3替换为33. 如果还停留在C ...