0

I have the following code. When I try to execute it I get

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'x': Unsatisfied dependency expressed through field 'dataSource': No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

My entry point:

@SpringBootApplication
public class Application {

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

    }
}

I tried adding in resources folder those lines in application.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/y
jdbc.username=a
jdbc.password=b

spring.datasource.url=jdbc:mysql://localhost:3306/y    spring.datasource.username=a
spring.datasource.password=b
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver

or I tried adding this context config:

<!-- Initialization for data source -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/cfa"/>
    <property name="username" value="cfa_user"/>
    <property name="password" value="cfa_pw"/>
</bean>

None of those worked. Any other ideas? Thanks!

I have to mention that my DAOs are extending

public abstract class BaseDAO extends NamedParameterJdbcDaoSupport {
    private @Autowired DataSource dataSource;

    @PostConstruct
    public void initDataSource(){
        setDataSource(dataSource);
    }
}

Dependencies

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mobile</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-social-facebook</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
tzortzik
  • 4,993
  • 9
  • 57
  • 88
  • 1
    Do you have the correct starter modules in your dependencies? – Florian Schaetz Aug 23 '16 at 18:59
  • I use maven. What should I have? – tzortzik Aug 23 '16 at 19:00
  • Possible duplicate: http://stackoverflow.com/questions/27843788/resource-annotation-no-qualifying-bean-of-type-javax-sql-datasource-is-define – Piotr Wilkin Aug 23 '16 at 19:13
  • If you use Maven, you should have Connector/J as a dependency: mysql mysql-connector-java 5.1.6 – Piotr Wilkin Aug 23 '16 at 19:14
  • I already had it included. I will update my post. – tzortzik Aug 23 '16 at 19:16
  • Try adding spring-boot-starter-jdbc instead of spring-jdbc, that should allow you to configure the datasource via the application.properties. – Florian Schaetz Aug 23 '16 at 19:29
  • After adding spring-boot-starter-jdbc, I keep getting `Unable to proxy method [public final javax.sql.DataSource org.springframework.jdbc.core.support.JdbcDaoSupport.getDataSource()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.` – tzortzik Aug 23 '16 at 19:38
  • What do you need the NamedParameterJdbcDaoSupport for anyway? Why not use a JDBCTemplate instead? – Florian Schaetz Aug 23 '16 at 19:43
  • In documentation they recommend using `JdbcDaoSupport` or `NamedParameterJdbcDaoSupport`. – tzortzik Aug 23 '16 at 19:45
  • Feel free, I never used it, see http://stackoverflow.com/questions/21519940/what-is-jdbcdaosupport-used-for . Anyway, that message isn't an exception, so it will probably work anyway... – Florian Schaetz Aug 23 '16 at 19:54

0 Answers0