Failure to Try to Inject Spring Boot Interfaces

Asked

Viewed 212 times

0

I’m developing a three-layer Rest api. Service, Repository and Domain, I injected the repository into the service using @Autowired but when I try to inject the service into the controller in the same way it triggers an Exception

Description:

Field veiculoService in br.fc.test.controller.VeiculoController required a bean of type 'br.fc.teste.service.IVeiculoService' 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 'br.fc.teste.service.IVeiculoService' in your configuration.

Repository

package br.fc.test.repository;

import java.util.List;

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

import br.fc.teste.domain.Veiculo;

@Repository
public interface IVeiculoRepository extends JpaRepository<Veiculo, Long> {

    List<Veiculo> findAll();
}

Interface Service

package br.fc.teste.service;

import java.util.List;

import br.fc.teste.domain.Veiculo;

public interface IVeiculoService {

    public List<Veiculo> findAll();
}

Service implementation

package br.fc.teste.service.implementation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.fc.test.repository.IVeiculoRepository;
import br.fc.teste.domain.Veiculo;
import br.fc.teste.service.IVeiculoService;

@Service
public class VeiculoService implements IVeiculoService {

    @Autowired
    private IVeiculoRepository veiculoRepository;

    @Override
    public List<Veiculo> findAll() {
        return veiculoRepository.findAll();
    }

}

Controller

package br.fc.test.controller;

import java.util.List;

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

import br.fc.teste.domain.Veiculo;
import br.fc.teste.service.IVeiculoService;

@RestController
@RequestMapping("/api/veiculo")
public class VeiculoController {

    @Autowired
    private IVeiculoService veiculoService;

    @GetMapping
    public  List<Veiculo>  GetAll() {
        return veiculoService.findAll();
    }
}
  • Maybe related: https://answall.com/questions/401093/no-qualifying-bean-of-type-ajuda-com-esse-error/401364#401364

2 answers

0

Include the @Component annotation on your interface this way:

package br.fc.teste.service;

import java.util.List;

import br.fc.teste.domain.Veiculo;

import org.springframework.stereotype.Component;

@Component
public interface IVeiculoService {

    public List<Veiculo> findAll();
}

0

Have more classes in your application? Another implementation for the vehicle service? I created a project with the same classes that you posted here and it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.