Java BeanFactory 未初始化或已关闭 - 在通过 ApplicationContext 访问 bean 之前调用“刷新”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21821988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:53:01  来源:igfitidea点击:

BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

javamultithreadingspringjavabeansapplicationcontext

提问by eclipznightz

I have this web.xml file

我有这个 web.xml 文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml /WEB-INF/core-spring-beans.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

and applicationContext that include the Bean

和包含 Bean 的 applicationContext

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <tx:annotation-driven transaction-manager="txManager"/>


    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource"/>


  <jee:jndi-lookup id="dataSource" jndi-name="pull_DS" resource-ref="true"/>

  <bean id="sqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
        p:configLocation="classpath:pull/DBUtils/sqlMapConfig.xml"
        p:dataSource-ref="dataSource"/>

  <bean id="daoBase" abstract="true"
        p:sqlMapClient-ref="sqlMapClient"/>

  <bean id="Photo"
        class="pull.DBUtils.PopPhotoImpl"
        parent="daoBase"/>

  <bean id="Trunkcate"
        class="pull.DBUtils.TrunkcateCifSrcImpl"
        parent="daoBase"/>

    <bean id="applicationContextProvder"class="pull.DBUtils.ApplicationContextProvider"/>     
</beans>

and the ApplicationContextProvider.java for ApplicationContext

和 ApplicationContextProvider.java 的 ApplicationContext

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext ctx = null;

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }

    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        this.ctx = ctx;
    }
}

AND so I defined a method returnCleanUp in TrunkcateCifSrcImpl Bean as

所以我在 TrunkcateCifSrcImpl Bean 中定义了一个方法 returnCleanUp 作为

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;


public class TrunkcateCifSrcImpl extends iBatisTemplate implements TrunkcateCifSrc{


public int retTrunkcateReady() {
     return super.trunkcateReady("CifSrc_Trunkcate.cleanUp");
}

public static int returnCleanUp() {
    TrunkcateCifSrcImpl trunkcate = null;
    int result;
    try {
        ApplicationContext ctx = ApplicationContextProvider
                .getApplicationContext();
        trunkcate = (TrunkcateCifSrcImpl) ctx.getBean("Trunkcate");
        result = trunkcate.retTrunkcateReady();
    }
    catch (NoSuchBeanDefinitionException noBeanE) {
        throw noBeanE;
    } catch (BeansException beanE) {
        throw beanE;
    }
    catch (Exception e) {
        throw e;
    }
    return result;
}

}

The iBatisTemplate class to return a value of map "trunkcateReady" (value returns back from iBatis function called from database)

iBatisTemplate 类返回映射“trunkcateReady”的值(从数据库调用的 iBatis 函数返回的值)

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;

public class iBatisTemplate extends SqlMapClientDaoSupport {

    public int trunkcateReady(final String id) {
        Map<String, Integer> map = new HashMap<String, Integer>(1);
        getSqlMapClientTemplate().queryForObject(id, map);
        return map.get("trunkcateReady");
    }

  }

but now once in awhile if i call this method from Trunkcate bean I would receive that exceptionm IllegalStateException, does anyone know what can I do to fix this??

但现在偶尔如果我从 Trunkcate bean 调用这个方法,我会收到那个异常 IllegalStateException,有谁知道我能做些什么来解决这个问题?

do{
    cleanUp = TrunkcateCifSrcImpl.returnCleanUp();
    if(cleanUp==1)
    {       
        logger.debug("Going to sleep........");
        Thread.sleep(65000);
    }
}while(cleanUp!=0);

Stack trace exception I got, this usually happens the first time it go through the loop then after Thread.sleep(65000), it will go back in normally

我得到堆栈跟踪异常,这通常发生在它第一次通过循环然后在 Thread.sleep(65000) 之后,它会正常返回

2014-02-14 12:23:45,646 GMT-0800 [managedServer=weeManagedServer-1]pull.quartzScheduler.PullingPDRTask - Pull failed....
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
    at pull.DBUtils.TrunkcateCifSrcImpl.returnCleanUp(TrunkcateCifSrcImpl.java:21)
    at pull.quartzScheduler.PullingPDRTask.excute(PullingPDRTask.java:257)
    at pull.quartzScheduler.SchedulerService.executePullingPDRTask(SchedulerService.java:31)
    at pull.quartzScheduler.PullStatefulJob.executeInternal(PullStatefulJob.java:42)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)

Thanks! any input would be helpful. I deploy my app on weblogic server 1036jdk1.7. Using spring 3.1.1. Thanks all!

谢谢!任何输入都会有所帮助。我将我的应用程序部署在 weblogic 服务器 1036jdk1.7 上。使用弹簧 3.1.1。谢谢大家!

回答by Jithesh Chandra

I had a similar Issue when i worked on Spring MVC using Tomcat 6 but i had by mistake declared multiple bean classes with the same name , check your classes once for ambiguity . Please post the complete stack trace .

我在使用 Tomcat 6 处理 Spring MVC 时遇到了类似的问题,但我错误地声明了多个具有相同名称的 bean 类,请检查您的类是否有歧义。请发布完整的堆栈跟踪。

Just In case if it helps this was my mistake so i got that error . Annotation-specified bean name 'countryServiceImpl' for bean class [com.ssrv.fms.service.contact.impl.CountryServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.ssrv.fms.service.admin.impl.CountryServiceImpl]

以防万一,如果它有帮助,这是我的错误,所以我得到了那个错误。bean 类 [com.ssrv.fms.service.contact.impl.CountryServiceImpl] 的注解指定 bean 名称“countryServiceImpl”与相同名称和类 [com.ssrv.fms.service.admin] 的现有不兼容 bean 定义冲突.impl.CountryServiceImpl]

回答by liyuhui

You can check the target folder of your project, and check if the configuration files are there.

您可以检查项目的目标文件夹,并检查配置文件是否在那里。

And I think you should tell more details of the errorInfo, it will be helpful.

而且我认为你应该告诉更多细节errorInfo,这会有所帮助。

I've met the same problem you have. But I had more detail error information under the

我遇到了和你一样的问题。但是我在下面有更详细的错误信息

BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

It says

它说

Caused by: java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL because it does not exist

So I check the target folder of the project, the "spring" folder is missing, which all of my configuration files in it. Although I don't know why it's not in there.

所以我检查了项目的目标文件夹,“spring”文件夹不见了,我所有的配置文件都在里面。虽然我不知道为什么它不在那里。

So I delete the target folder of the project, and rebuild it. And problem's solved. The 'spring' folder appeared.

所以我删除了项目的目标文件夹,并重建它。问题解决了。出现了“spring”文件夹。

Before and after:

之前和之后:

beforeafter

前后