Vineel Pellella
Vineel Pellella

Reputation: 422

Consider defining a bean of type 'Dao interface' in your configuration

I am planning to break all the DAO layer code into a separate Spring boot data project. So I created two projects one of which will have all the database related code and the other will have service code which will then use the first project as a dependency to interact for any database-related actions.

Project1: DatabaseInteractionService Project2: InsuranceCleanupService

InsuranceCleanupService startup class

package com.ics;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EntityScan(basePackages = {"com.ms.base.entity", "com.dis", "com.dis.config", "com.dis.dao", "com.dis.dao.Impl" })

public class InsuranceCleanupServiceApplication {

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

}

DatabaseInteractionService startup class

package com.dis;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@SpringBootApplication
@EntityScan("com.ms.base.entity")
public class DatabaseInteractionServiceApplication {

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


    @Value("spring.datasource.driverClassName")
    String driverClassName;
    
    @Value("spring.datasource.url")
    String url;
    
    
    @Value("spring.datasource.username")
    String username;
    
    @Value("spring.datasource.password")
    String password;
    
    @Bean
    public DataSource dataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    
    @Bean
    public JdbcTemplate jdbcTemplate()
    {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }
}

I am using the Mysql database and Spring-data. When worked independently there seems no issue. After separation, I am facing the below issue.

Consider defining a bean of type 'com.dis.dao.EmployeeDao' in your configuration.

Upvotes: 1

Views: 792

Answers (1)

pleft
pleft

Reputation: 7905

Why you have 2 @SpringBootApplication classes since one project will be used as dependency? It seems to me that DatabaseInteractionServiceApplication is more a @Configuration class rather than a @SpringBootApplication.

Apart from this the error

Consider defining a bean of type 'com.dis.dao.EmployeeDao' in your configuration

means that somewhere you inject (@Autowire) a EmployeeDao bean but it doesn't exist in your Spring context. I suppose it exists in InsuranceCleanupServiceApplication project since there you have annotated:

@EntityScan(basePackages = {"com.ms.base.entity", "com.dis", "com.dis.config", "com.dis.dao", "com.dis.dao.Impl" })

@EntityScan annotation works only for @Entity annotated classes. If you want to scan for services/repositories/components you should change it to @ComponentScan. You need to add @ComponentScan since your @SpringBootApplication class belongs to a different package, com.ics than com.dis and as such it won't automatically pick up classes that are annotated with @Service, @Component or @Repository belonging to com.dis package. So I suppose just

@ComponentScan(basePackages = {"com.dis"})

will suffice since it will also scan subpackages e.g. "com.dis.config", "com.dis.dao", "com.dis.dao.Impl"

Leave @EntityScan only for your entity package (it already exists in DatabaseInteractionServiceApplication):

@EntityScan("com.ms.base.entity")

Upvotes: 1

Related Questions