【问题标题】:Mybatis-spring-boot - cant use datasource that was injected as beanMybatis-spring-boot - 无法使用作为 bean 注入的数据源
【发布时间】:2016-12-12 12:42:12
【问题描述】:

下面是我的配置类。

@Configuration
@Component
class Config {
@Bean
@ConfigurationProperties(prefix = "my.spring.datasource")
public javax.sql.DataSource dataSource() {

    return DataSourceBuilder
            .create()
            .build();
}

}  

我正在使用 Spring boot 和 spring-boot-mybatis-starter。我的application.properties 文件已正确完成。请记住,我遵循这个:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

但是,在启动过程中出现错误:

- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.type) did not find property 'spring.datasource.type'
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'   

你能帮帮我吗?

当我排除 DataSourceAutoConfiguration 时,我得到:

- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'  

当我删除数据源 bean 并删除所有数据源属性中的前缀 my. 时,一切正常。

编辑

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a bean of type 'org.apache.ibatis.session.SqlSessionFactory' that could not be found.
    - Bean method 'sqlSessionFactory' in 'MybatisAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.apache.ibatis.session.SqlSessionFactory; SearchStrategy: all) found bean 'sqlSessionFactory'


Action:

Consider revisiting the conditions above or defining a bean of type 'org.apache.ibatis.session.SqlSessionFactory' in your configuration.   

application.properties

my.spring.datasource.driverClassName=***
my.spring.datasource.url=****
my.spring.datasource.username=user
my.spring.datasource.password=****
my.spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource

【问题讨论】:

  • 为什么要配置自己的数据源?为什么不使用 Spring Boot 基于 spring.datasource 属性提供的那个呢?此外,消息与您的数据源无关,因为您的配置中应该有名为 my.spring.datasource.whatever 的属性。此外,@Configuration 不应使用 @Component 注释。
  • 你为什么要配置自己的数据源? 既然如此,我将定义路由动态数据源(并将其交给 mybatis 并能够选择数据库)。 此外,这些消息与您的数据源无关,因为您的配置中应该有名为 my.spring.datasource.whatever 的属性我认为是这样,但不知道如何修复它。
  • 您不需要修复任何东西...这些消息是针对您未配置的默认数据源的。
  • 但由于这些错误,应用程序无法启动
  • 不,那些不是错误。那些是匹配和否定匹配不是错误......你的错误在于其他地方并且与默认数据源无关。但是,如上所述,您的 @Configuration 不应该是 @Component

标签: java spring spring-boot mybatis


【解决方案1】:

确保您已在application.yml 中配置了以下属性

spring:
  datasource:
    url: jdbc:oracle:thin:@db-host:port/catalog
    username: user
    password: pass
    driver-class-name: oracle.jdbc.OracleDriver

一旦你这样做了,你就可以将DataSource 注入到一个sping 组件; 如下图。

@Autowired
DataSource dataSource;

【讨论】:

  • 总的来说,我没有application.yml,只有application.properties。我应该创建它吗?如何创建它?
  • 是的。您已经创建了一个 application.properties 并将上述配置添加到其中。
猜你喜欢
  • 2018-01-27
  • 1970-01-01
  • 2016-02-12
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多