springboot指定basePackages后项目启动报错Failed to configure a DataSource: ‘url‘ attribute is not specified...

朱雀 2022-11-07 04:11 323阅读 0赞

由于项目引用了额外的插件包,但插件的包名和项目包名不一致,插件包不在启动类包下,springboot不会自动扫描相关组件类。

指定了basePackages为插件包名后,启动报错(之前并未指定,说明默认扫描了启动类所在包)。

报错信息,大概是说找不到数据库配置:

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  6. Reason: Failed to determine suitable jdbc url
  7. Action:
  8. Consider the following:
  9. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  10. If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active).

试错试了半天才恍然大悟,原来我把项目主包给弄丢了,我只配了插件的额外包,没有配项目主包路径,

导致springboot只扫描额外的插件包,项目主包不扫描,启动找不到数据库配置,真是捡了芝麻丢了西瓜!

由此能看出,指定了basePackages后,springboot只会扫描我们指定的包,而不会扫描之前默认的启动类所在包。

所以,指定basePackages时,得把需要扫描的所有包全都配置上。

最终的启动类:

  1. package com.ylb;
  2. @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
  3. @ComponentScan(basePackages = { "com.ylb", "com.yilabao.plugin" })
  4. public class ApplicationServer {
  5. public static void main(String[] args) {
  6. SpringApplication app = new SpringApplication(ApplicationServer.class);
  7. app.addListeners(new ApplicationStartingEventListener());
  8. app.run(args);
  9. }
  10. }

关键代码:

  1. @ComponentScan(basePackages = { "com.ylb", "com.yilabao.plugin" })

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读