Home > Back-end >  Consider defining a bean of type 'com.example.filter.FilterDao' in your configuration
Consider defining a bean of type 'com.example.filter.FilterDao' in your configuration

Time:06-17

I am trying to connect spring boot application with MySQL for that I have created a interface with name FilterDao which extend JpaRepository class. but whenever I try to make object of implemented class in Service I got this error "Consider defining a bean of type 'com.example.filter.FilterDao' in your configuration" as I am new to spring boot I don't understand this error.

FilterApplication.java

package com.example.filter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class FilterApplication {

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

}

FilterDao.java

package com.example.filter;

import com.example.filter.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.FluentQuery;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

//@Configuration
public interface FilterDao extends JpaRepository<Filter, Integer> {

}

FilterService.java

package com.example.filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class FilterService {
    @Autowired
    private FilterDao filterDao;

    public List<Filter> getData() {
        System.out.println("----------------------HERE-------------");
        return filterDao.findAll();
    }
}

FilterConnector.java

package com.example.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FilterConnector {

    @Autowired
    private FilterService filterService;
    @GetMapping("/home")
    public List<Filter> home()
    {

        return this.filterService.getData();
    }
}

Project Structure enter image description here

CodePudding user response:

Annotate FilterDao with @Repository

CodePudding user response:

Seems spring has not created bean for FilterDao repository and you are trying to use that`

@Autowired private FilterDao filterDao;`

There might different reasons for this exception. Please try the below solution.

  1. Use @EnableJpaRepositories(basePackages = "com.example.filter") with your FilterApplication class.
  2. Use @ComponentScan(basePackages = "com.example.*") with FilterApplication class
  3. Use @Repoitory annotation with FilterDao interface.

Hope this helps. For more details check the below tutorial.

https://www.netsurfingzone.com/jpa/consider-defining-a-bean-of-type-in-your-configuration/

  • Related