【发布时间】: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