SpringBoot 项目:Singleton bean creation not allowed while singletons of this factory are in destruct解决

本文记录了启动Spring Boot项目时遇到的端口被占用问题及其解决过程。具体表现为因8081端口被其他进程占用导致项目无法正常启动,并通过命令行工具定位占用端口的进程,最终关闭该进程解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.event.ApplicationListenerMethodAdapter.getTargetBean(ApplicationListenerMethodAdapter.java:280) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:250) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:174) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:137) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:389) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:994) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:961) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.cloud.context.named.NamedContextFactory.destroy(NamedContextFactory.java:76) [spring-cloud-context-1.2.4.RELEASE.jar:1.2.4.RELEASE]
	at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:272) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1009) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:961) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
	at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:794) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
	at com.aspire.comp.ivr.client.MainApplication.main(MainApplication.java:28) [classes/:na]

以上是本人起动项目时报的错误!

在往上翻日志会找到导致这个错误其它错误日志,如下:

java.net.BindException: Address already in use: bind

没错,端口被占用!本人使用的8081端口,windows系统

查询端口占用情况:

window+R组合键,调出命令窗口。输入cmd回车

输入命令netstat -ano回车

C:\Users\my_name>netstat -ano

活动连接

  协议  本地地址          外部地址        状态           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       304
  TCP    0.0.0.0:371            0.0.0.0:0              LISTENING       2952
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:8081           0.0.0.0:0              LISTENING       5176
  TCP    0.0.0.0:10029          0.0.0.0:0              LISTENING       3144
  TCP    0.0.0.0:33333          0.0.0.0:0              LISTENING       2844
  TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING       712

以上是部分端口占用情况,其中包含8081.

接下来查看是谁占用了该端口:输入指令tasklist|findstr "5176"(5176是对应进程的PID,在行后)

C:\Users\zhanghao_d>tasklist|findstr "5176"
java.exe                      5176 Services                   0    504,352 K

 

 

### 错误分析 在 Spring 框架中,`Singleton bean creation not allowed while singletons of this factory are in destruction` 是一种常见的生命周期管理错误。此错误通常发生在应用程序关闭期间尝试创建新的单例 Bean 的情况下。具体到 `registrationListener` 这一场景,可能的原因包括: 1. **Bean 生命周期冲突**:某些 Bean 在销毁阶段触发了其他 Bean 的初始化逻辑[^1]。 2. **资源竞争或重复绑定**:例如端口已被占用或其他外部依赖未正确释放[^4]。 3. **测试环境中的特殊行为**:如果是在 Test 测试环境中遇到该问题,则可能是由于上下文多次加载引起的[^2]。 --- ### 解决方案 #### 方法 1: 调整 Bean 初始化顺序 可以通过设置 `@DependsOn` 或调整配置类的加载顺序来确保 `registrationListener` 不会在不恰当的时间点被调用。例如,在定义 `registrationListener` 时指定其依赖关系: ```java @Component @DependsOn({"otherRequiredBeans"}) public class RegistrationListener { public RegistrationListener() { // Initialization logic here } } ``` 这种方法可以防止因依赖缺失而导致的非法状态访问。 --- #### 方法 2: 避免在销毁方法中请求新 Bean 检查是否存在任何代码片段试图在 Bean 的销毁方法 (`destroyMethod`) 中获取其他 Bean 实例。这种操作违反了 Spring 容器的设计原则,并可能导致上述错误。以下是修正示例: ```java @Bean(destroyMethod = "close") public MyResource myResource() { return new MyResource(); } // 修改 close 方法以避免再次请求容器内的 Bean public void close() { // Do cleanup without requesting additional beans } ``` 通过这种方式可有效规避潜在的风险[^3]。 --- #### 方法 3: 处理端口冲突(仅适用于运行时) 如果问题是由于端口冲突引起(如引用 所述),则可以在应用启动前动态分配可用端口或将默认端口号更改为未使用的值。修改 `application.properties` 文件即可完成这一更改: ```properties server.port=0 # 动态分配随机空闲端口 ``` 或者直接设定固定但不同的端口号: ```properties server.port=8090 ``` --- #### 方法 4: 使用独立的应用程序上下文实例化 对于单元测试而言,频繁重新加载整个 ApplicationContext 可能引发此类问题。推荐采用缓存机制减少不必要的重建次数;也可以考虑基于 Mock 技术模拟部分组件的行为从而降低对外部真实对象的需求程度。 --- ### 总结 针对当前描述的情况,“singleton bean creation not allowed” 主要源于 Bean 生命周期管理和资源配置不当所致。建议按照以上四种策略逐一排查并修复根本原因所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值