SpringContextHolder工具类---通过springContextHolder静态注入实体Bean

/**
 * Copyright ©  All rights reserved.
 */
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext = null;

    private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(requiredType);
    }

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     */
    public static void clearHolder() {
        if (logger.isDebugEnabled()) {
            logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
        }
        applicationContext = null;
    }

    /**
     * 实现ApplicationContextAware接口, 注入Context到静态变量中.
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }

    /**
     * 实现DisposableBean接口, 在Context关闭时清理静态变量.
     */
    @Override
    public void destroy() throws Exception {
        SpringContextHolder.clearHolder();
    }

    /**
     * 检查ApplicationContext不为空.
     */
    private static void assertContextInjected() {
        Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
    }
}

通过此工具类解决在静态方法中需要使用到Bean实例,而Autowired注入的实例不支持在静态方法中使用的问题.~ 可以根据name或.class对象获取Bean实例

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Common Lang和Common Lang3是Apache软件基金会的两个开源Java库,它们提供了很多有用的工具类和方法,可以帮助我们简化Java编程。在本文中,我们将介绍如何使用Common Lang / Common Lang3中的一些常用工具类。 1. 字符串操作 a) StringUtils类 StringUtils类提供了很多有用的方法来处理字符串。以下是一些常用方法: - 判断字符串是否为空或null ``` StringUtils.isEmpty(str) StringUtils.isBlank(str) // 包括空格、制表符、换行符等 ``` - 拼接字符串 ``` StringUtils.join(strArray, separator) ``` - 替换字符串 ``` StringUtils.replace(str, searchStr, replaceStr) ``` - 判断字符串是否是数字 ``` StringUtils.isNumeric(str) ``` - 截取字符串 ``` StringUtils.substring(str, start, end) ``` b) WordUtils类 WordUtils类提供了一些用于处理单词的方法,例如: - 将字符串转换为首字母大写或小写 ``` WordUtils.capitalize(str) WordUtils.uncapitalize(str) ``` - 将字符串转换为标题样式,即每个单词的首字母大写 ``` WordUtils.capitalizeFully(str) ``` 2. 数组操作 a) ArrayUtils类 ArrayUtils类提供了一些有用的方法来操作数组。以下是一些常用方法: - 判断数组是否为空或null ``` ArrayUtils.isEmpty(array) ``` - 数组拷贝 ``` ArrayUtils.copy(array) ``` - 数组查找 ``` ArrayUtils.indexOf(array, objectToFind) ``` - 数组反转 ``` ArrayUtils.reverse(array) ``` b) StringUtils类同样提供了一些有用的方法来操作字符串数组。 - 字符串数组转换为字符串 ``` StringUtils.join(strArray, separator) ``` - 按照分隔符将字符串转换为字符串数组 ``` StringUtils.split(str, separator) ``` 3. 随机数生成 a) RandomUtils类 RandomUtils类提供了一些方法来生成随机数。以下是一些常用方法: - 生成指定范围内的随机整数 ``` RandomUtils.nextInt(min, max) ``` - 生成指定长度的随机字符串 ``` RandomUtils.nextString(length) ``` 4. 日期处理 a) DateUtils类 DateUtils类提供了一些方法来处理日期。以下是一些常用方法: - 获取当前日期 ``` DateUtils.now() ``` - 日期格式化 ``` DateUtils.format(date, pattern) ``` - 日期加减 ``` DateUtils.addDays(date, amount) DateUtils.addMonths(date, amount) DateUtils.addYears(date, amount) ``` b) DateFormatUtils类 DateFormatUtils类提供了一些方法来格式化日期。以下是一些常用方法: - 将日期格式化为指定格式的字符串 ``` DateFormatUtils.format(date, pattern) ``` - 将当前日期格式化为指定格式的字符串 ``` DateFormatUtils.format(System.currentTimeMillis(), pattern) ``` 以上就是Common Lang / Common Lang3中一些常用的工具类和方法的介绍。这些工具类和方法可以帮助我们更快捷、更高效地编写Java程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值