Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DataSource cannot be autowired in spring boot application?

I know that spring boot will create a dataSource Bean automatically if related configurations are set in application.properties, like:

spring.datasource.url = jdbc:mysql://192.168.10.103:3306/hms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=test@123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Application code:

package com.synline.mdataserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.apache.tomcat.jdbc.pool.DataSource;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    AnnotationConfigApplicationContext context;

    /*@Autowired
    DataSource dataSource;*/

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        DataSource dataSource = (DataSource)context.getBean("dataSource");
        System.out.println(dataSource);

        while (true) {
           Thread.sleep(5000);
        }

    }
}

If the @Autowired DataSource is commented out, the Bean information will be printed:

org.apache.tomcat.jdbc.pool.DataSource@1800a575{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; ....}

So I think Spring Boot really created the Bean.

But if @Autowried DataSource is used, exception occurs to complain No Such Bean

Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.apache.tomcat.jdbc.pool.DataSource com.synline.mdataserver.Application.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tomcat.jdbc.pool.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
like image 632
Zhengqiang Zhu Avatar asked Oct 12 '15 01:10

Zhengqiang Zhu


People also ask

Why Autowired is not working in spring boot?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

What is the difference between Autowired and resource?

@Autowired is a spring annotation whereas @Resource is specified by the JSR-250. So the latter is part of normal java where as @Autowired is only available by spring.

How do you enable Autowiring in spring boot?

If you want properly use @Autowired in your spring-boot application, you must do next steps: Add @SpringBootApplication to your main class. Add @Service or @Component annotation to class you want inject. Use one of two ways that you describe in question, to autowire.


1 Answers

Your variable should be declared as a standard JDBC DataSource (i.e. javax.sql.DataSource), not as a particular implementation of that interface.

like image 182
JB Nizet Avatar answered Oct 03 '22 19:10

JB Nizet