java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh‘ befo

小鱼儿 2022-08-31 11:04 227阅读 0赞

问题描述:
springmvc框架项目在调用异步任务时,抛异常:

  1. java.lang.IllegalStateException: BeanFactory not initialized or already closed
  2. - call 'refresh' before accessing beans via the ApplicationContext

经过各种查资料,得知应该是xml配置问题,各种验证,确实是配置问题:spring-context.xml配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
  3. <context:component-scan base-package="data.installmethod.util.*"/>
  4. <task:executor id="testExecutor1" pool-size="5" />
  5. <task:annotation-driven executor="testExecutor1" />
  6. <task:executor id="testExecutor2" pool-size="5" />
  7. <task:annotation-driven executor="testExecutor2"/>
  8. </beans>

验证时,xml这两组配置,保留一组则项目启动正常,保留两组,则启动时抛异常!问题在于<task:annotation-driven />这个标签,这个配置文件一个时就没问题。task:annotation-driven/标签还是@EnableAsync注解,最终的目的就是向容器注入AsyncAnnotationBeanPostProcessor后处理器,@EnableAsync注解可以替代task:annotation-driven/标签开启异步任务支持的功能,所以改变思路,配置文件调整为如下,对,注释掉task:annotation-driven/标签内容,用注解方式:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
  3. <context:component-scan base-package="data.installmethod.util.*"/>
  4. <task:executor id="testExecutor1" pool-size="5" />
  5. <!-- <task:annotation-driven executor="testExecutor1" /> -->
  6. <task:executor id="testExecutor2" pool-size="5" />
  7. <!-- <task:annotation-driven executor="testExecutor2"/> -->
  8. </beans>

用@EnableAsync注解方式开启异步任务支持的功能,代码如下

  1. package com.test.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.scheduling.annotation.Async;
  5. import org.springframework.scheduling.annotation.EnableAsync;
  6. import org.springframework.stereotype.Component;
  7. import com.test.OssUtil;
  8. @Component
  9. @EnableAsync
  10. public class AsyncTestTask {
  11. private static final Logger logger = LoggerFactory.getLogger(AsyncTestTask.class);
  12. @Async("testExecutor1")
  13. public void test1(String filefullname,byte[] videobyte) {
  14. logger.warn("{}|{}","saveVideoBase64ToOss",Thread.currentThread().getName());
  15. OssUtil.uploadByteAry("", filefullname, videobyte);
  16. }
  17. @Async("testExecutor2")
  18. public void test2(String filefullname, String image) {
  19. logger.warn("{}|{}","savePhotoBase64ToOss",Thread.currentThread().getName());
  20. OssUtil.uploadStr("", filefullname, image);
  21. }
  22. }

这样,就解决了项目启动报错问题

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读