Home > front end >  spring boot constructor parameter could not be found
spring boot constructor parameter could not be found

Time:09-22

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);
    }

}

CodePudding user response:

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.

CodePudding user response:

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

( sry not native )

  • Related