Springboot~多个数据源时自定义datasource的bean
当我们的项目里有多种数据源时,mysql,h2,mongodb,es等等,这时你需要为每种数据源添加自己的bean类,在bean里构建配置,而直接在yml里的配置往往会出现循环引用的问题。 org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'scopedTarget.dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference? 有时,也会出现其它奇葩的问题,总之,当你的项目里数据源比较多时,通过配置文件+profile来管理它们更清晰,也不容易出错。 为h2添加配置bean /** * 多数据源时,H2的datasource声明 */ @Configuration @ActiveProfiles("test") public class H2Mock { @Value("${h2.dbName:Test1}") String dbName; @Value("${h2.dbSchema:db/schema.sql}") String dbSchema; @Bean