springboot指定basePackages后项目启动报错Failed to configure a DataSource: ‘url‘ attribute is not specified...
由于项目引用了额外的插件包,但插件的包名和项目包名不一致,插件包不在启动类包下,springboot不会自动扫描相关组件类。
指定了basePackages为插件包名后,启动报错(之前并未指定,说明默认扫描了启动类所在包)。
报错信息,大概是说找不到数据库配置:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine suitable jdbc url
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
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时,得把需要扫描的所有包全都配置上。
最终的启动类:
package com.ylb;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan(basePackages = { "com.ylb", "com.yilabao.plugin" })
public class ApplicationServer {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ApplicationServer.class);
app.addListeners(new ApplicationStartingEventListener());
app.run(args);
}
}
关键代码:
@ComponentScan(basePackages = { "com.ylb", "com.yilabao.plugin" })
还没有评论,来说两句吧...