原创作品,出自 “晓风残月xj
博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。

由于各种原因,可能存在诸多不足,欢迎斧正!

1、ApplicationContextAware

org.springframework.context.ApplicationContextAware

    实现该接口的类,可以在spring容器初始化的时候调用setApplicationContext方法,从而获得ApplicationContext中的所有bean。

  1. public class SpringUtils implements ApplicationContextAware {
  2. private static ApplicationContext applicationContext;
  3. @Override
  4. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  5. SpringUtils.applicationContext = applicationContext;
  6. synchronized (SpringUtils.class) {
  7. SpringUtils.class.notifyAll();
  8. }
  9. }
  10. private static void checkForInitialized() {
  11. if (applicationContext == null) {
  12. synchronized (SpringUtils.class) {
  13. if (applicationContext == null) {
  14. try {
  15. SpringUtils.class.wait();
  16. } catch (InterruptedException e) {
  17. Thread.currentThread().interrupt();
  18. }
  19. }
  20. }
  21. }
  22. }
  23. @SuppressWarnings("unchecked")
  24. public static <T> T bean(String name) {
  25. checkForInitialized();
  26. return (T) applicationContext.getBean(name);
  27. }
  28. public static <T> T bean(Class<T> clazz) {
  29. checkForInitialized();
  30. return applicationContext.getBean(clazz);
  31. }
  32. public static <T> Map<String, T> beans(Class<T> clazz) {
  33. checkForInitialized();
  34. return applicationContext.getBeansOfType(clazz);
  35. }
  36. }
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
synchronized (SpringUtils.class) {
SpringUtils.class.notifyAll();
}
} private static void checkForInitialized() {
if (applicationContext == null) {
synchronized (SpringUtils.class) {
if (applicationContext == null) {
try {
SpringUtils.class.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
} @SuppressWarnings("unchecked")
public static &lt;T&gt; T bean(String name) {
checkForInitialized();
return (T) applicationContext.getBean(name);
} public static &lt;T&gt; T bean(Class&lt;T&gt; clazz) {
checkForInitialized();
return applicationContext.getBean(clazz);
} public static &lt;T&gt; Map&lt;String, T&gt; beans(Class&lt;T&gt; clazz) {
checkForInitialized();
return applicationContext.getBeansOfType(clazz);
}

}

2、DisposableBean、InitializingBean
  import org.springframework.beans.factory.DisposableBean;

  import org.springframework.beans.factory.InitializingBean;

  接口实现初始化方法和销毁前操作主要有下面3种方法:

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

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

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

  1. public class PersonService  implements InitializingBean,DisposableBean{
  2. private String  message;
  3. public String getMessage() {
  4. return message;
  5. }
  6. public void setMessage(String message) {
  7. this.message = message;
  8. }
  9. @Override
  10. public void destroy() throws Exception {
  11. // TODO Auto-generated method stub
  12. System.out.println("I'm  init  method  using implements InitializingBean interface...."+message);
  13. }
  14. @Override
  15. public void afterPropertiesSet() throws Exception {
  16. // TODO Auto-generated method stub
  17. System.out.println("I'm  init  method  using implements DisposableBean interface...."+message);
  18. }
  19. }
public class PersonService  implements InitializingBean,DisposableBean{
private String  message;

public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("I'm init method using implements InitializingBean interface...."+message); } @Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("I'm init method using implements DisposableBean interface...."+message); }

}

  路漫漫其修远兮,很多时候感觉想法比较幼稚。首先东西比较简单,其次工作也比较忙,还好周末可以抽时间处理这个。由于相关知识积累有限,欢迎大家提意见斧正,在此表示感谢!后续版本会持续更新…

Spring常用工具类(ApplicationContextAware、DisposableBean、InitializingBean)的更多相关文章

  1. 2015第30周三Spring常用工具类

    文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...

  2. Spring常用工具类

    Spring框架下自带了丰富的工具类,在我们开发时可以简化很多工作: 1.Resource访问文件资源: 具体有: ResourceUtils.getFile(url); FileSystemReso ...

  3. Spring 常用工具类

    1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...

  4. spring中常用工具类介绍

    http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作     Spring 定义了一个 org.springframework.core.io ...

  5. 简单了解Spring中常用工具类_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...

  6. Maven基础&&Spring框架阶段常用工具类整理

    常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...

  7. commons-lang3-3.2.jar中的常用工具类的使用

    这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...

  8. commons-lang常用工具类StringEscapeUtils使用--转

    https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是 ...

  9. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

随机推荐

  1. [TypeScript] Shallow copy object by using spread opreator

    For example we have an object: const todo = { text: "Water the flowers", completed: false, ...

  2. jQuery Easy UI Panel(面板)组件

    panel(面板)组件,跟前面的组件使用方法差点儿都差点儿相同,也是从设置一些面板属性.操作面板触发的事件.我们可针对面板对象的操作方法这三个点去学习. 后面有一些组件要依赖于这个组件. 另一点跟前面 ...

  3. Vue router的query对象里的值的问题

    在使用 $router.push() 时,如果使用了query,则可以在跳转后从query中获取到对应的参数.如果传的是字符串自然没问题,但是如果传的其他类型的数据,在跳转之后是正常的,而跳转之后再刷 ...

  4. NOIP2016 天天爱跑步(线段树/桶)

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.天天爱跑步是一个养成类游戏,需要 玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 N个结点 ...

  5. 22. Spring Boot 动态数据源(多数据源自动切换)

    转自:https://blog.csdn.net/catoop/article/details/50575038

  6. 【JAVASE】Java同一时候抛出多个异常

    Java有异常抛出后.跳出程序.一般无法运行接下来的代码. 大家做登陆功能.常常会实username和password的登陆校验,username或者password错误.假设通常是提示usernam ...

  7. java方法调用之动态调用多态(重写override)的实现原理——方法表(三)

    上两篇篇博文讨论了java的重载(overload)与重写(override).静态分派与动态分派.这篇博文讨论下动态分派的实现方法,即多态override的实现原理. java方法调用之重载.重写的 ...

  8. C#基础数据类型与字节数组(内存中的数据格式)相互转换(BitConverter 类)

      在某种通讯协议中(如 Modbus),可能需要把一些基本的数据类型内存中的表示形式转换成以字节数组的形式,方便传送.C/C++中可以利用指针等操作完成,但C#中没有指针,咋办呢?可以用BitCon ...

  9. arduino串口输出问题

  10. DC针对pipeline的优化

    set_optimize_register    true compile  -ultra 调整pipleline各级的组合逻辑,使得各级组合逻辑的延迟跟接近 对非pipeline进行优化: regi ...