解决“Exception in thread main java.lang.IllegalStateException BeanFactory not initialized ”的办法

本文分享了一位Spring框架初学者在使用过程中遇到的常见错误:未初始化BeanFactory或已关闭,提示在访问bean前调用'refresh'。博主在main方法中忘记传入xml配置文件,导致应用程序无法找到bean定义。通过正确加载配置文件,问题得以解决。
部署运行你感兴趣的模型镜像

​ 博主最近刚开始接触了Spring框架,用的还不是特别到熟,经常会碰到错误,下面给大家分享一下博主遇到的小问题

抛出问题
Exception in thread "main" 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:170)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
	at com.wanbang.autowire.Main.main(Main.java:10)

以上是控制台报错信息

接下来我们来看一下main方法

小伙伴们有发现什么错误吗?是的,我们没有传入xml文件为参数到ClassPathXmlApplicationContext方法中,初学者是不是很容易忘记呀!

现在我们加上xml文件

这样就解决这个问题了

小结

对于Spring初学者来说,还是要多敲,才能把握住小细节

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

Annotation-specified bean name 'RiskScreeningThresholdService' for bean class [com.axatp.enonmotor.service.impl.RiskScreeningThresholdServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.axatp.enonmotor.bo.service.impl.RiskScreeningThresholdBoServiceImpl] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349) at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287) at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:90) at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74) at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1391) at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1371) at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:179) at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:149) at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:511) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391) ... 22 more 2025-12-11 11:00:24.802 ERROR [StandardContext] One or more listeners failed to start. Full details will be found in the appropriate container log file 2025-12-11 11:00:24.803 ERROR [StandardContext] Context [/enonhealth] startup failed due to previous errors 2025-12-11 11:00:24.820 INFO [[/enonhealth]] Closing Spring root WebApplicationContext 2025-12-11 11:00:24.891 ERROR [[/enonhealth]] Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener 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:168) at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1108) at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1077) at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1023) at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:515) at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:112) at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4900) at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5537) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:149) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
最新发布
12-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值