spring使用DataSoure注入参数时报No supported DataSource type found

    平常阅读源码什么的没有目的性,所以很少去看什么源码,主要是比较绕看起来吃力,所以一般工作只是找个模版模仿一下。

以上废话,割————————————————————————————————————————————————————————————

    最近照常模仿使用了其它项目里的DataSource用法,代码如下:

    

 1    @Bean
 2     @Primary
 3     @ConfigurationProperties(prefix = "datasource.from")
 4     public DataSource dataSource(){
 5         return DataSourceBuilder.create().build();
 6     }
 7     
 8     @Autowired
 9     @Qualifier("dataSource")
10     private DataSource dataSource;
11 
12     @Bean(name="sqlSessionFactory")
13     public SqlSessionFactory sqlSessionFactory() throws Exception {
14         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
15         bean.setDataSource(dataSource);
16         bean.setConfigLocation(new ClassPathResource("MybatisConfig.xml"));
17         return bean.getObject();
18     }

    在运行初始化的时候报“No supported DataSource type found”,查找网上资料又说没有指定数据库类型的。

    于是,自己在配置中增加了datasource.from.type=oracle,运行还是报一样的错。于是,到晚上脑子清醒点时,看了下DataSource初始化时的部分源码。

    主要的是第三段代码如下:

1 private static final String[] DATA_SOURCE_TYPE_NAMES = { "org.apache.tomcat.jdbc.pool.DataSource", "com.zaxxer.hikari.HikariDataSource", "org.apache.commons.dbcp.BasicDataSource", "org.apache.commons.dbcp2.BasicDataSource" };
public Class<? extends DataSource> findType(){
    if (this.type != null) {
        return this.type;
    }
    for (String name : DATA_SOURCE_TYPE_NAMES) {
    try {
        return ClassUtils.forName(name, this.classLoader);
    }catch (Exception ex) {}
1  private Class<? extends DataSource> getType() {
2       Class<? extends DataSource> type = findType();
3       if (type != null) {
4         return type;
5       }
6       throw new IllegalStateException("No supported DataSource type found");
7     }
1 public DataSource build() {
2      Class<? extends DataSource> type = getType();
3      DataSource result = (DataSource)BeanUtils.instantiate(type);
4      maybeGetDriverClassName();
5      bind(result);
6      return result;
7    }

    看完上面的代码就明白为什么报错了,就是说在创建DataSource没有找到javax.sql.DataSource的子类,也就是必须要引入DATA_SOURCE_TYPE_NAMES枚举中涉及类的jar包,另外注意引入对应数据库JDBC的jar包。

 

以上,Good lucky!

 

转载于:https://www.cnblogs.com/knowledgebird/p/5251925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值