前提:
springboot使用eureka作为注册中心,并提供接口,在项目中使用多数据源时,启动报错,报错提示无法注入dao,具体报错信息如下:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-05-25 09:46:19.431 ERROR 11376 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field loginDaoMapper in com.ccx.axis.system.service.impl.LoginServiceImpl required a bean of type 'com.ccx.axis.system.dao.LoginDaoMapper' that could not be found.
Action:
Consider defining a bean of type 'com.ccx.axis.system.dao.LoginDaoMapper' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:61942', transport: 'socket'
Process finished with exit code 1
正常情况下该报错的解决方案有两个,具体如下:
1、将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,就不会报无法注入问题了。
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解,如下:
@EnableEurekaClient
@ComponentScan(value = "com.ccx.axis.*")//需要添加需要扫的包
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class AxisLoginControllerApplication {
public static void main(String[] args) {
SpringApplication.run(AxisLoginControllerApplication.class, args);
}
}
但是我这两个都有做,还是报上面的错误,这我就比较奇怪了,折腾一下午之后发现是因为少写了一个注解,即加载多数据源配置的注解没有添加上去,添加上去之后就完美解决问题了。这个需要大家注意,完整的内容如下:具体这个注解是干嘛的,多数据源怎么配,请自行百度,谢谢
@EnableEurekaClient
@ComponentScan(value = "com.ccx.axis.*")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@Import(DynamicDataSourceRegister.class)//重要
public class AxisLoginControllerApplication {
public static void main(String[] args) {
SpringApplication.run(AxisLoginControllerApplication.class, args);
}
}