Repository Application Startup Failure Not Found When Starting Springboot

Asked

Viewed 105 times

1

I am trying to run my spring boot project with mysql connection and this giving the following failure

Description: Error Starting Applicationcontext. To display the conditions report re-run your application with 'debug' enabled. 2019-11-21 19:17:49.956 ERROR 15737 --- [ restartedMain] o.s.b.d.Loggingfailureanalysisreporter :


APPLICATION FAILED TO START


Description:

Field er in com.eventosapp.eventosapp.Eventocontroller required a bean of type 'com.eventosapp.Repository.Eventorepository' 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 'com.eventosapp.Repository.Eventorepository' in your Configuration.

My Eventocontroller

package com.eventosapp.eventosapp;

import com.eventosapp.repository.EventoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


public class EventoController {

    @Autowired
    private EventoRepository er;

    @RequestMapping(value = "/cadastrarEvento", method = RequestMethod.GET)
    public String form() {

        return "formEvento";
    }

    @RequestMapping(value = "/cadastrarEvento", method = RequestMethod.POST)
    public String form(Evento evento) {

        er.save(evento);
        return "redirect:/cadastrarEvento";
    }

}

My Dataconfiguration

package com.eventosapp.eventosapp;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;


@Configuration
public class DataConfiguration {


    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/eventosapp");
        dataSource.setUsername("root"); 
        dataSource.setPassword("0000");

      return dataSource;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); 
        adapter.setDatabase(Database.MYSQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        adapter.setPrepareConnection(true);

        return adapter;


    }

}

My Class Event

package com.eventosapp.eventosapp;

import java.io.Serializable;

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


@Entity
public class Evento implements Serializable {
    private static final long serialVersionUID = 1L;

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private long codigo;

    public long getCodigo() {
        return codigo;
    }

    public void setCodigo(long codigo) {
        this.codigo = codigo;
    }

    private String nome;
    private String local;
    private String data;
    private String horario;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getHorario() {
        return horario;
    }

    public void setHorario(String horario) {
        this.horario = horario;
    }

}

My Repositorry Interface

package com.eventosapp.repository;

import org.springframework.data.repository.CrudRepository;

import com.eventosapp.eventosapp.Evento;



public interface EventoRepository extends CrudRepository<Evento,String> {

}

NOTE: I tried to attach in the Eventocontroller class the following code snippet but it didn’t work

@EnableJpaRepositories(basePackages = "com.eventosapp.repository.EventoRepository")
@SpringBootApplication

1 answer

1

I believe your Repository interface is missing the @Repository annotation

import package org.springframework.stereotype.Repository;

package com.eventosapp.repository;

import org.springframework.data.repository.CrudRepository;

import com.eventosapp.eventosapp.Evento;

import org.springframework.stereotype.Repository;


@Repository
public interface EventoRepository extends CrudRepository<Evento,String> {}

https://www.baeldung.com/spring-data-repositories

Browser other questions tagged

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