0
votes

below is my configuration class.

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

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

}  

I am using Spring boot and spring-boot-mybatis-starter. My application.properties file is properly completed. Keep in mind, that I follow this: http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

However, during launching I get error:

- 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'   

Can you help me, please ?

When I excluce DataSourceAutoConfiguration then I get:

- 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'  

When I drop datasource bean and remove prefix my. in all datasouece properties everything is ok.

Edit

***************************
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
1
Why are you configuring your own datasource? Why not use the one provided by Spring Boot based on the spring.datasource properties? Also the messages aren't about YOUR datasource as yu should have properties named my.spring.datasource.whatever in your configuration. Also @Configuration shouldn't be annotated with @Component. - M. Deinum
Why are you configuring your own datasource? Since, I am going to define Routed Dynamic datasource (and give it to mybatis and be able to choose database). Also the messages aren't about YOUR datasource as yu should have properties named my.spring.datasource.whatever in your configuration I think so, however no idea how to fix it. - user6023611
You shouldn't need to fix anything... Those message are for the default datasource which you don't configure. - M. Deinum
But application doesn't start by these errors - user6023611
No those aren't errors. Those are matches and negative matches NOT errors... Your mistake lies else where and has nothing to do with the default datasource. However as stated your @Configuration shouldn't be a @Component. - M. Deinum

1 Answers

0
votes

make sure that you have configured the following properties in application.yml

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

Once you do that, you will be able to inject DataSource to a sping component; as shown below.

@Autowired
DataSource dataSource;