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);
	}
	
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值