首页 技术 正文
技术 2022年11月19日
0 收藏 899 点赞 3,733 浏览 2682 个字

最近,配置项目,使用SpringBoot2.2.1,配置mybatis访问db,配好后,使用自定义的数据源。启动发生:

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 a suitable driver classAction: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 (no profiles are currently active).

  虽然,我知道,是因为我没有在application.yml文件中配置:spring.datasource。但是因为我是自定义的数据源,配置项不在spring.datasource,所以希望,不配置spring.datasource,也能启动项目。单纯在@SpringBootApplication(exclude = {DataSourceAutoConfigure.class})中排除DataSourceAutoConfigure是不行的。

下面是解决方案:

方案1:

在自定义数据源中,添加自定义自动配置类。见下面的例子:

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;/**
* 自定义自动配置类
* 1. 自定义数据源,自定义数据源是为了,防止因为没有在配置文件中,配置spring.datasource,
* 出现:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
* 2. 切面类,用来切换主从数据源
*
*/
@EnableAspectJAutoProxy
@Configuration
public class CustomDataSourceAutoConfiguration { @Bean
@ConditionalOnMissingBean
public DataSource defaultDataSource() {
return new CustomDataSource());
}
} @Bean
@ConditionalOnBean({DataSource.class})
public CustomDataSourceAspect CustomDsAspect() {
return new CustomDataSourceAspect();
}
}

  别忘了,在META-INF/spring.factories文件中配置:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xxx.framework.datasource.masterslave.CustomDataSourceAutoConfiguration

  方案2:

在启动类上,添加如下代码:

import com.lzj.framework.datasource.multi.MultiDatabaseMapperFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan(basePackages = {"com.xxx.test.dao"}, factoryBean = MultiDatabaseMapperFactoryBean.class)
public class LzjFrameworkTestApplication { public static void main(String[] args) {
SpringApplication.run(LzjFrameworkTestApplication.class, args);
}}

  第一要在@SpringBootApplication中排除DataSourceAutoConfiguration类的配置;第二,要配置@MapperScan,在参数中指出,扫描标准@Mapper注解的dao接口的包位置,和自定义factoryBean的实现类。factoryBean需要自己实现,需要继承MapperFactoryBean接口。

当然,你要是不使用数据库,只需把DataSourceAutoConfiguration排除就可以了。

项目,就可以正常启动了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896