java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh‘ befo
Posted xiejunna
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh‘ befo相关的知识,希望对你有一定的参考价值。
问题描述:
springmvc框架项目在调用异步任务时,抛异常:
java.lang.IllegalStateException: BeanFactory not initialized or already closed
- call 'refresh' before accessing beans via the ApplicationContext
经过各种查资料,得知应该是xml配置问题,各种验证,确实是配置问题:spring-context.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<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">
<context:component-scan base-package="data.installmethod.util.*"/>
<task:executor id="testExecutor1" pool-size="5" />
<task:annotation-driven executor="testExecutor1" />
<task:executor id="testExecutor2" pool-size="5" />
<task:annotation-driven executor="testExecutor2"/>
</beans>
验证时,xml这两组配置,保留一组则项目启动正常,保留两组,则启动时抛异常!问题在于<task:annotation-driven />
这个标签,这个配置文件一个时就没问题。task:annotation-driven/标签还是@EnableAsync注解,最终的目的就是向容器注入AsyncAnnotationBeanPostProcessor后处理器,@EnableAsync注解可以替代task:annotation-driven/标签开启异步任务支持的功能,所以改变思路,配置文件调整为如下,对,注释掉task:annotation-driven/标签内容,用注解方式:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<context:component-scan base-package="data.installmethod.util.*"/>
<task:executor id="testExecutor1" pool-size="5" />
<!-- <task:annotation-driven executor="testExecutor1" /> -->
<task:executor id="testExecutor2" pool-size="5" />
<!-- <task:annotation-driven executor="testExecutor2"/> -->
</beans>
用@EnableAsync注解方式开启异步任务支持的功能,代码如下
package com.test.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import com.test.OssUtil;
@Component
@EnableAsync
public class AsyncTestTask {
private static final Logger logger = LoggerFactory.getLogger(AsyncTestTask.class);
@Async("testExecutor1")
public void test1(String filefullname,byte[] videobyte) {
logger.warn("{}|{}","saveVideoBase64ToOss",Thread.currentThread().getName());
OssUtil.uploadByteAry("", filefullname, videobyte);
}
@Async("testExecutor2")
public void test2(String filefullname, String image) {
logger.warn("{}|{}","savePhotoBase64ToOss",Thread.currentThread().getName());
OssUtil.uploadStr("", filefullname, image);
}
}
这样,就解决了项目启动报错问题
以上是关于java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh‘ befo的主要内容,如果未能解决你的问题,请参考以下文章