Autowire не работает с контроллером Spring Boot - PullRequest
3 голосов
/ 15 февраля 2020

Всякий раз, когда я пытаюсь автоматически связать собственный репозиторий, реализующий репозиторий JPA в моем классе контроллера, он не может этого сделать и выдает ошибку «no bean def found found», тогда как если я делаю то же самое с любым классом обслуживания, то он работает нормально. Может кто-нибудь объяснить мне, почему это так?


Spring Boot 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2020-02-15 13:01:50.169 ERROR 16304 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

    ***************************
    APPLICATION FAILED TO START
    ***************************

    Description:

    Field customerRepo in Controllers.MainController required a bean of type 'Repository.CustomerRepo' 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 'Repository.CustomerRepo' in your configuration.

    ```
    @SpringBootApplication
    @ComponentScan(basePackages = "Controllers")
    public class DemoApplication {

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

    }
    ``````````````````
    @RestController
    @RequestMapping("/")
    public class MainController {

        @Autowired
        private CustomerRepo customerRepo;

        @RequestMapping(value = "/home", method = RequestMethod.GET)
        public String homePage() {
            Customer testCustomer = new Customer();
            testCustomer.setFirstName("csdcsdccs");
            testCustomer.setLastName("csdcsdccs");
            testCustomer.setMiddleName("csdcsdccs");
            testCustomer.setAddressLine("csdcsdccs");
            testCustomer.setCountry("csdcsdccs");
            testCustomer.setPincode(713201);
            testCustomer.setState("csdcsdccs");
            testCustomer.setDateOfBirth(new Date(2019, 5, 13));

            customerRepo.save(testCustomer);

            return "inserted";
        }
    }

    `````````````
    @Repository
    public interface CustomerRepo extends CrudRepository<Customer, Long> {

    }
    ``````````````````````````


Ответы [ 2 ]

0 голосов
/ 16 февраля 2020

Можете ли вы попробовать @Component или @Repository аннотации на вашем репо?

0 голосов
/ 15 февраля 2020

Приложение Spring не может сканировать хранилище. Не могли бы вы проверить, определено ли хранилище, определенное в базовом пакете, иначе добавьте пакет хранилища хранилища также в @ComponentScan.

@SpringBootApplication
@ComponentScan(basePackages = {"Controllers","Repository"})
public class DemoApplication {

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

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...