自己写了一个工具类实现ApplicationContextAware接口方便在一些特定的情形无法使用spring容器里面的bean

Caused by: java.lang.IllegalStateException: applicaitonContext未注入
	at com.wangzunbin.infra.utils.SpringContextHolder.assertContextInjected(SpringContextHolder.java:90) ~[classes/:?]
	at com.wangzunbin.infra.utils.SpringContextHolder.getBean(SpringContextHolder.java:40) ~[classes/:?]
	at com.wangzunbin.infra.application.integration.server.MqttServerCallback.<clinit>(MqttServerCallback.java:32) ~[classes/:?]
	at com.wangzunbin.infra.application.integration.client.JwhMqttClient.create(JwhMqttClient.java:111) ~[classes/:?]
	at com.wangzunbin.infra.application.integration.client.JwhMqttClient.connect(JwhMqttClient.java:48) ~[classes/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_40]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_40]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_40]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_40]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 24 more
	... 24 more

这是系统失败注入情况下的debug截图 , 竟然是null

上面意思没有applicationContext没有被实例, 所以我们到实例applicationContext的代码上并加上断点, 再重新debug, 而失败的情况是即使打了断点, 还没执行重写的setApplicationContext方法就抛出异常了

后来思考了一下应该是spring扫描包的顺序问题, 先扫描SpringContextHolder的包(com.xxx.xxx.infra), 再扫其他的包, 重新启动直接ok了

 SpringContextHolder类如下:

@Lazy(false)
@Component
@Slf4j
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {


    private static ApplicationContext applicationContext = null;

    /**
     * 从静态变量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);
    }

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

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     */
    public static void clearHolder() {
        if (log.isDebugEnabled()){
            log.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不为空.
     */
    public static boolean hasContextInjected() {
        return applicationContext != null;
    }

    /**
     * 检查ApplicationContext不为空.
     */
    private static void assertContextInjected() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入");
        }
    }
}

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐