Search code examples
springspring-boothibernatejpaspring-data-jpa

Getting error as "required a bean of type.....that could not be found"


I have made a basic structure of a project named expense tracker, where I got error. Even though I have been trying to resolve it for a long time, it persists.

Error :


APPLICATION FAILED TO START


Description:

Field categoryRepository in com.app.expensetracker.service.CategoryServiceImpl required a bean of type 'com.app.expensetracker.dao.CategoryRepository' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.app.expensetracker.dao.CategoryRepository' in your configuration.

CategoryRepository

package com.app.expensetracker.dao;

import com.app.expensetracker.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryRepository extends JpaRepository<Category, String> {

}

Category

package com.app.expensetracker.entity;

import jakarta.persistence.*;
import lombok.*;
import java.util.List;

@Entity
@Table(name = "category")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString

public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="cat_id",nullable = false)
    private String cat_id;

    @Column(name="categoryName")
    private String categoryName;

    @Column(name="users")
    private List<String> users;

//    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
//    @JoinColumn(name = "user_id")
//    private  User user;

}

CategoryService

package com.app.expensetracker.service;

import com.app.expensetracker.dto.CategoryCreateRequest;
import com.app.expensetracker.entity.Category;
import org.springframework.http.ResponseEntity;
import java.util.Optional;

public interface CategoryService {

    Category createCategory(CategoryCreateRequest categoryCreateRequest);
    ResponseEntity<Optional<Category>> getCategoryById(String category_id);
    Category updateCategory(String categoryId, CategoryCreateRequest categoryCreateRequest);
    ResponseEntity<Optional<Category>> deleteCategoryById(String category_id);
}

CategoryServiceImpl

package com.app.expensetracker.service;
import com.app.expensetracker.dao.CategoryRepository;
import com.app.expensetracker.dto.CategoryCreateRequest;
import com.app.expensetracker.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
private CategoryRepository categoryRepository;

    @Override
    @Transactional
    public Category createCategory(CategoryCreateRequest categoryCreateRequest){
        Category category = categoryCreateRequest.to();
        return categoryRepository.save(category);
    }
    
    
    @Override
    @Transactional
    public ResponseEntity<Optional<Category>> getCategoryById(String category_id){
    
        Optional<Category> category =  categoryRepository.findById(category_id);
        if(category == null){
            return ResponseEntity.notFound().build();
        }
        else{
            return ResponseEntity.ok(category);
        }
    
    }
    
    @Override
    @Transactional
    public Category updateCategory(String categoryId, CategoryCreateRequest categoryCreateRequest){
        Category category = categoryCreateRequest.to();
        return categoryRepository.save(category);
    }
    
    @Override
    @Transactional
    public ResponseEntity<Optional<Category>> deleteCategoryById(String category_id){
        Optional<Category> category =  categoryRepository.findById(category_id);
        if(category == null){
            return ResponseEntity.notFound().build();
        }
        else{
            categoryRepository.deleteById(category_id);
            return ResponseEntity.ok(category);
        }
    }

}

ExpenseTrackerApplication

package com.app.expensetracker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication //(exclude = {DataSourceAutoConfiguration.class })
//@ComponentScan(basePackages = "com.app.*")
//@EntityScan("com.app.*")
@EnableJpaRepositories(basePackages = "com.app.expensetracker.repository")

public class ExpenseTrackerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ExpenseTrackerApplication.class, args);
    }

}

Solution

  • Before

         @SpringBootApplication //(exclude = {DataSourceAutoConfiguration.class })
         //@ComponentScan(basePackages = "com.app.*")
         //@EntityScan("com.app.*")
         @EnableJpaRepositories(basePackages = "com.app.expensetracker.repository")
         public class ExpenseTrackerApplication{
    

    On disabling the @EnableJpaRepositories annotation in the main class,the application started working.

    After

     @SpringBootApplication
     public class ExpenseTrackerApplication{
    

    In my case, this worked for me.