`
kavy
  • 浏览: 866458 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring 容器初始化 bean 和销毁三种方式

 
阅读更多

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

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

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

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

 

下面演示通过  @PostConstruct 和 @PreDestory

1:定义相关的实现类:

 

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

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

 

 

[html] view plaincopy
 
  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.   
  10. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->  
  11.   
  12. <context:annotation-config />  
  13.   
  14. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
  15.   <property name="message" value="123"></property>  
  16. </bean>  
  17.   
  18. </beans>  

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

 

测试类:

 

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

测试结果:

 

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

 

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

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

只需要修改配置文件为:

 

[html] view plaincopy
 
  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.   
  10. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->  
  11.   
  12. <!-- <context:annotation-config /> -->  
  13.   
  14.   
  15. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  
  16. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
  17.           <property name="message" value="123"></property>  
  18. </bean>  
  19.   
  20.   
  21.   
  22. </beans>  

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

 

更多3
1
分享到:
评论

相关推荐

    spring容器初始化bean和销毁bean之前进行一些操作的方法

    NULL 博文链接:https://bijian1013.iteye.com/blog/2374256

    Spring Bean的初始化和销毁实例详解

    主要介绍了Spring Bean的初始化和销毁,结合实例形式详细分析了Spring Bean的初始化和销毁相关配置、使用方法及操作注意事项,需要的朋友可以参考下

    Spring bean初始化及销毁你必须要掌握的回调方法.docx

    3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...

    Spring Bean初始化及销毁多种实现方式

    主要介绍了Spring Bean初始化及销毁多种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    08-IoC配置-bean的生命周期控制

    Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务。 Spring IOC容器对Bean的生命周期...在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法。

    spring.xls

    spring的IOC: ... * 初始化 * 由spring容器调用init方法 * 在构造函数之后执行 * 销毁 * 如果是单例,则必须返回ClassPathXmlApplicationContext该容器,才能执行销毁工作 * 如果是多例,容器不负责销毁

    Spring教程  主要内容:介绍Spring的历史,Spring的概论和它的体系结构,重点阐述它在J2EE中扮演的角色。

    作者:钱安川(Moxie) ...Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22

    spring培训-笔记

    Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22 注:后面的...

    spring.doc

    Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定...

    深入解析Spring IoC源码:核心机制与实践应用

    本文深入探讨了Spring框架中IoC容器的源码机制,涵盖了容器的初始化、Bean工厂的实例化、Bean定义的读取及Spring Bean的生命周期管理。通过精细的分析,本文揭示了AnnotationConfigApplicationContext的实例化过程,...

    Spring Bean 的生命周期

    为了便于这些工作的设计,Spring IoC 容器提供了相关的功能,可以让应用定制 Bean 的初始化和销毁过程。 Bean 生命周期 的 init-method 属性或 @Bean 注解的 initMethod 属性),那么将调用该方法。 如果存在与 ...

    SSH笔记-IOC容器中 Bean 的生命周期

    SSH笔记-IOC容器中 Bean 的生命周期,通过实现BeanPostProcessor接口,来定义初始化方法和销毁方法时的逻辑

    spring1.2学习心得分享

    初始化:&lt;bean init-method=""/&gt; e.资源释放:&lt;bean destroy-method=""/&gt;仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用...

    spring学习心得

    初始化:&lt;bean init-method=""/&gt; e.资源释放:&lt;bean destroy-method=""/&gt;仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用...

    开源框架面试专题及答案.pdf

    &gt; Spring 框架提供了以下四种方式来管理 bean 的生命周期事件: &gt; InitializingBean 和 DisposableBean 回调接口 &gt; 针对特殊行为的其他 Aware 接口 &gt; Bean 配置文件中的 Custom init()方法和 destroy()方法 &gt; @...

    spring1.1开发理解

    初始化:&lt;bean init-method=""/&gt; e.资源释放:&lt;bean destroy-method=""/&gt;仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用...

    Spring IOC Bean标签属性介绍(教学视频+源代码)

    Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml 1.2 实体类JavaBean 1.2.1 User类 ...1.9.1 实体类JavaBean User加自定义的初始化方法和销毁方法 1.9.3 加了lazy

    Spring 3 Reference中文

    4.4.4 延迟初始化bean . 55 4.4.5 自动装配协作者.. 55 4.4.5.1 自动装配的限制和缺点.. 56 4.4.5.2 从自动装配中排除bean. 57 4.4.6 方法注入. 57 4.4.6.1 查找方法注入.. 58 4.4....

    SpringBoot Bean的生命周期demo.zip

    一般情况下,我们只是关心如何正确地将Bean装配到容器中,并不关心Ioc容器是如何装配和销毁Bean的过程。但是恰恰有时候,我们需要自定义初始化或销毁Bean的过程,以满足一些“特殊的”需求。比如,数据源在关闭的...

Global site tag (gtag.js) - Google Analytics