Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

APPLICATION FAILED TO START

package br.com.alura.Spring.Data;

import java.util.Scanner;

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

import Service.CrudCargoService;

@SpringBootApplication
public class SpringDataApplication implements CommandLineRunner {

    private Boolean system = true;

    private final CrudCargoService service;

    public SpringDataApplication(CrudCargoService service) {
        this.service = service;
    }

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

    @Override
    public void run(String... args) throws Exception {

        Scanner sc = new Scanner(System.in);

        while (system) {

            System.out.print("Qual operaçao deseja ?");
            System.out.println("0 - sair");
            System.out.println("1 - cargo");

            int action = sc.nextInt();

            if (action == 1) {
                service.inicial(sc);
            }else {
                system = false;
            }
        }
    }
}
package Service;

import java.util.Scanner;

import org.springframework.stereotype.Service;

import br.com.alura.Spring.Data.orm.Cargo;
import repository.CargoRepository;

@Service
public class CrudCargoService {

    private final CargoRepository repository;

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

    public void inicial(Scanner sc) {
        salvar(sc);
    }

    private void salvar(Scanner sc) {
        System.out.print("Descricao do cargo: ");
        String descricao = sc.next();
        Cargo cargo = new Cargo();
        cargo.setDescricao(descricao);
        repository.save(cargo);
        System.out.println("salvo");
    }
}
package br.com.alura.Spring.Data.orm;

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 Cargo() {
    }

    public Cargo(Integer id, String descricao) {
        this.id = id;
        this.descricao = 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 repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import br.com.alura.Spring.Data.orm.Cargo;

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

Oi Wallace, tem o log de erro desse APPLICATION FAILED TO START para enviar?

tem sim.

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-01-20 12:16:49.960 ERROR 4588 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in br.com.alura.Spring.Data.SpringDataApplication required a bean of type 'Service.CrudCargoService' that could not be found.

Action:

Consider defining a bean of type 'Service.CrudCargoService' in your configuration.

solução!

Oi Wallace

tenta colocar sua classe "CrudCargoService "dentro de algum pacote tipo "package br.com.alura.spring.service;" ao invés de "package Service;" faça o mesmo com a classe "CargoRepository" colocando ela em "package br.com.alura.spring.repository;"

Otávio, muito obrigado, a questao era os pacotes mesmo, ja consegui resolver.