本文目錄
一、項目背景
今天配置了一個springboot項目,使用swagger2進行前後端分離開發。配置了swagger後竟然發現啓動失敗。
springboot (v2.1.8.RELEASE) + swagger (v2.8.0)
二、問題描述
***************************
APPLICATION FAILED TO START
***************************Description:
The bean 'createRestApi', defined in com.spring4all.swagger.SwaggerAutoConfiguration, could not be registered. A bean with that name has already been defined in class path resource [com/baidu/practice/config/swagger/SwaggerConfig.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
三、問題原因
在啓動項目時,createRestApi這個Bean已經存在,不能被註冊。導致啓動失敗。
四、解決方案
目前想到的解決辦法有兩個,方案1和方案2,以後如果有新的解決辦法再補充進來。也歡迎大家留言說出自己的想法。
方案1:重新命名
重新命名這個實例,例如本此問題把這個(createRestApi)名字改爲另一個名字即可。
方案2:修改配置文件
main: allow-bean-definition-overriding: true #當遇到同樣名字的時候,是否允許覆蓋註冊
如下配置爲正確的寫法
spring: application: name: test-springboot-service system: testspringboot main: allow-bean-definition-overriding: true # 意思是後來發現的bean會覆蓋之前相同名稱的bean profiles: active: local servlet: multipart: max-file-size: 1000MB max-request-size: 1000MB cloud: zookeeper: discovery: enabled: true register: true root: /iot-service connectString: devl.iot.com:52500 jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 logging: config: classpath:logback.xml