微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringContextHolder获取bean

前景,工具类的某个方法想要获取bean的实例,但是工具类的方法是static的,不能使用@Autowired 注入。想了一下,可以通过SpringContextHolder在工具类中使用

//示例调用代码代码
private static EmailService emailService = SpringContextHolder.getBean(EmailService.class);

1.SpringContextHolder的类,需要coding

package com.jamelLi.distributed.session.util;

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;

/**
 * @ClassName SpringContextHolder
 * @Description: 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext
 * @Auther: Jamel.Li
 * @Date: 2019/1/11 21:15
 * @version: 1.0
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, disposableBean {

    private static ApplicationContext applicationContext = null;

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


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

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


    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        checkApplicationContext();
        return applicationContext.getBean(requiredType);
    }
    
    /**
     * 发布事件
     */
    public static ApplicationContext publishEvent(Object event) {
        applicationContext.publishEvent(event);
        return applicationContext;
    }
    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     */
    public static void clearHolder() {
        applicationContext = null;
    }

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

    /**
     * @Description check applicationContext 是否为null
     * @Author Jamel.Li
     * @Date 2019/1/13 14:29
     * @Param []
     * @return void
     */
    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
        }
    }

}

2.在xml注入SpringContextHolder。

<!--使用xml 注入SpringContextHolder-->
<bean id="springContextHolder" class="com.jamelLi.distributed.session.util.SpringContextHolder" />  

ps:一般来说,项目的util包,是不会被spring扫描到的。如果SpringContextHolder注入失败,说明没有被spring 扫描,需要增加下面的配置

<!-- 使用Annotation自动注册Bean -->
 <context:component-scan base-package="com.jamelLi.distributed.session.util"></context:component-scan>

原文地址:https://www.jb51.cc/wenti/3285129.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐