DN学院讲师招募     Markdown编辑器轻松写博文     TOP 50 CTO坐镇直招     读文章说感想获好礼

分类: Spring 2013-03-16 16:48 24901人阅读 评论(1) 收藏 举报

关于在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 init的更多相关文章

  1. spring init method destroy method

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  2. spring web项目下,判断项目是否启动完成

    本文同时发布于见鬼网:https://faceghost.com/article/483341 概述:spring 加载完成后,我们有时候会做一些初始化工作,如加载一些缓存,DB,等,这里采用实现Se ...

  3. Spring Boot 如何极简入门?

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运行 ...

  4. Spring中使用要点集合

    1.InitializingBean和init-method方法 Spring的InitializingBean为bean提供了定义初始化方法的方式.InitializingBean是一个接口,它仅仅 ...

  5. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  6. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

  7. 创建Spring Boot项目的几种方式总结

    一.我们可以使用Spring Initializr来创建SpringBoot项目. Spring Initializr从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构.虽 ...

  8. Spring boot参考指南

    介绍 转载自:https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details 带目录浏览地址:http://ww ...

  9. spring boot cli 知识点

    spring boot cli 版本列表: https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/ spri ...

随机推荐

  1. JDBC-ODBC桥连接方式操纵SQL数据库

    /**  * 功能:演示使用JDBC-ODBC桥连接方式操纵SQL数据库  * 作者:徐守威  * 操作步骤:  * 1.配置数据源  * 2.在程序中连接数据源  * 3.操作数据  */ pack ...

  2. 使用spring的JavaMailSender发送邮件

    一:pom.xml <!-- java邮件 -->      <dependency>          <groupId>javax.mail</group ...

  3. easyui message show中msg嵌入一个按钮如何绑定事件

    http://www.oschina.net/question/945028_171927

  4. easyUI draggable组件使用

    easyUI draggable组件使用: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  5. 第一篇:CUDA 6.0 安装及配置( WIN7 64位 / 英伟达G卡 / VS2010 )

    前言 本文讲解如何在VS 2010开发平台中搭建CUDA开发环境. 当前配置: 系统:WIN7 64位 开发平台:VS 2010 显卡:英伟达G卡 CUDA版本:6.0 若配置不同,请谨慎参考本文. ...

  6. java算法 蓝桥杯 高精度加法

    问题描述 在C/C++语言中,整型所能表示的范围一般为-231到231(大约21亿),即使long long型,一般也只能表示到-263到263.要想计算更加规模的数,就要用软件来扩展了,比如用数组或 ...

  7. 【puthon基础】之str类字符串

    str类字符串是不可变对象 1.创建字符串 s1 = str() #创建一个空字符串 s2 = str("hello") #创建字符串"hello" 2.处理字 ...

  8. 双击startup.bat启动tomcat时闪退原因及解决方案

    在启动免安装tomcat时,很容易遇到双击startup.bat,启动窗口闪退而tomcat服务未启动的状况. 具体原因:tomcat在启动时,需要读取环境变量和配置信息,如果缺少Java环境,即缺少 ...

  9. 通过web sql实现增删查改

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  10. Zore copy(翻译《Efficient data transfer through zero copy》)

    原文:https://www.ibm.com/developerworks/library/j-zerocopy/ <Efficient data transfer through zero c ...