Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro - Application required a bean of type - Não encontra o bean Repository

Olá pessoal! Meu código está pedindo o bean da classe/interface Repository... Já tentei usar @Autowired e nada...

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

Description:

Parameter 0 of constructor in br.com.spring.data.Application required a bean of type 'br.com.spring.repository.CargoRepository' that could not be found.


Action:

Consider defining a bean of type 'br.com.spring.repository.CargoRepository' in your configuration.

package br.com.spring.data;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import br.com.spring.data.model.Cargo;
import br.com.spring.repository.CargoRepository;

@SpringBootApplication
public class Application implements CommandLineRunner {

    private CargoRepository repository;

    public Application(CargoRepository repository) {
        this.repository = repository;
    }

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

    @Override
    public void run(String... args) throws Exception {
        Cargo cargo = new Cargo();
        cargo.setDescricao("DESENVOLVEDOR DE SOFTWARE");

        repository.save(cargo);
    }

}

package br.com.spring.data.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "cargos")
public class Cargo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String descricao;


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getDescricao() {
        return descricao;
    }


    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

}

package br.com.spring.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import br.com.spring.data.model.Cargo;

@Repository
public interface CargoRepository extends CrudRepository<Cargo, Integer>{

}
1 resposta
solução!

Já descobri, o problema era nos pacotes.

Meu main estava no pacote:

package br.com.spring.data;

O model Cargo no pacote:

package br.com.spring.data.model;

E o problema estava aqui, o Repository estava em um pacote diferente, tive que colocar ".data.repository", aí funcinou.

package br.com.spring.repository;