0

I am working on spring boot app with tutorial. I did everything like guy from tutorial but still have problem with some constructor:(

The error is: Parameter 0 of constructor in com.wewtorek.shop.controllers.AdminController required a bean of type 'com.wewtorek.shop.models.data.PageRepository' that could not be found.

Code is:

package com.wewtorek.shop.controllers;
import com.wewtorek.shop.models.data.Page;
import com.wewtorek.shop.models.data.PageRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private PageRepository pageRepository;


    public AdminController(PageRepository pageRepository) {
        this.pageRepository = pageRepository;
    }

    @GetMapping
    public String admin(Model model) {

        List<Page> pages = pageRepository.findAll();

        model.addAttribute("pages", pages);

        return "admin";
    }
}

PageRepository:

package com.wewtorek.shop.models.data;

import org.springframework.data.jpa.repository.JpaRepository;

public interface PageRepository extends JpaRepository<Page, Integer> {
}

Application:

package com.wewtorek.shop;

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

@SpringBootApplication
public class ShopApplication {

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

}
micha123
  • 1
  • 1

2 Answers2

1

First :

@Repository is missing

@Repository
public interface PageRepository extends JpaRepository<Page, Integer> {
}

Doc : https://www.baeldung.com/spring-data-repositories

You dont have to create an constructor in controller : It should be something like this :

    public class AdminController{

    @Autowired
    private PageRepository pageRepository;


    --- Code ---
    }

@Autowired instanciate a service, you dont have to build it

BUT you have to put @Repository or @Service to use @Autowired

I take this example from my school project :

Controller

In my LoanService i call another server but u can replace it by u'r repository

Service

And last tips i promise :D, a complete NoSQL school project i did https://github.com/juju630/ClientServeurNoSQL

( sry not native )

  • indeed i forgot to add @Repository but it still does not work:( – micha123 Sep 22 '22 at 09:00
  • Well , in second time you should remove this constructor and use @Autowired, recommend to take a look from baeldung doc : ) – Julien BEAUDOUX Sep 22 '22 at 22:57
  • @Repository is not required in Spring Boot applications. Beans with a singular constructor are automatically attempted for autowiring for a few years now. – Darren Forsythe Sep 22 '22 at 23:22
  • Autowired inherently means field injection which is not recommended by Spring. OP used constructor injection which ideally should be used. – Ankit Sharma Sep 23 '22 at 04:49
0

Without looking at your project, this is going to be hard to give a definitive solution.

What is happening is when spring tries to create the bean AdminController it can not find a unique bean as the dependency PageRepository.

A few things to look at to try to solve this

Is the bean JpaRepository<Page, Integer> annotated correctly for spring to pick it up and create an instance?

Is the bean JpaRepository<Page, Integer> being scanned by spring?

What is your package structure? this can be very important for the default scanning of spring beans.

To investigate you could add a default constructor to allow it to ignore the dependency, then debug out all beans on startup of your app using the answers Here

I hope this helps.

MartinByers
  • 1,240
  • 1
  • 9
  • 15