多数据源配置时出现的⼀些问题
⾃⼰写了⼀个SpringBoot结构的⼩项⽬,练习⼀下多数据源的配置。因为是从之前的项⽬中拷贝的代码,不清楚其实现原理,因此出现了⼀些错误。
⼀、Premature end of file.
控制台输出如下:
Caused by: NestedIOException: Failed to parse mapping resource: 'file
[E:\githubproject\rong_system\target\classes\mapping\rong\boke\l]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause:
按照字⾯意思是“⽂件提前结束”,问题出现在这个l。⽹上说原因是因为有没有正确结束的标签。但是所有的基础代码都是⽤generator插件⾃动⽣成的,没做任何改动,不会出现语法错误。分析⼀下,原来是因为我注释掉了这个⽂件。当时为了解决其他错误,缩⼩排查范围,
直接注释掉了整个l⽂件。springBoot 扫描的时候,能扫描到⽂件名,但是⽂件内容是空⽩的,因此直接报错。
⼆、more than one 'primary' bean found among candidates
控制台输出如下:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'systemReadDataSource' defined in class path resource
[com/byk/rong/system/config/SystemReadDataSourceConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: more than one 'primary' bean found among candidates: [bokeWriteDataSource, bokeReadDataSource, systemWriteDataSource, systemReadDataSource]
很明显,出现了不⽌⼀个的“primary”,出primary出现的位置。⼀共有四个数据源配置⽂件在其中两
个⽂件中⽤到了
@Primary注解,试着删掉其中⼀个⽂件中的@Primary注解,项⽬正常启动。
三、required a single bean, but 4 were found
问题⼆⾥⾯删掉了⼀个@Primary项⽬能启动,那要是删除所有的@Primary注解呢。
控制台输出如下:
Description:
Parameter 0 of method sqlSessionFactory in fig.SystemReadDataSourceConfiguration required a single bean, but 4 were found:
- bokeWriteDataSource: defined by method 'dataSource' in class path resource
[com/byk/rong/boke/config/BokeWriteDataSourceConfiguration.class]
- bokeReadDataSource: defined by method 'dataSource' in class path resource
[com/byk/rong/boke/config/BokeReadDataSourceConfiguration.class]
- systemWriteDataSource: defined by method 'dataSource' in class path resource
[com/byk/rong/system/config/SystemWriteDataSourceConfiguration.class]
datasource是什么意思
- systemReadDataSource: defined by method 'dataSource' in class path resource
[com/byk/rong/system/config/SystemReadDataSourceConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using
@Qualifier to identify the bean that should be consumed
字⾯意思“要求有⼀个单独的bean ,但是到了四个”,就是说springboot已经分不清需启动时需要加载哪个bean了。最下⾯也给出了修改意见,加⼀个@Primary或者@Qualifier注解。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。