sgarcia.dev
sgarcia.dev

Reputation: 6160

NoSuchBeanDefinitionException while building a simple Spring JPA project

I'm following along the following tutorial: https://spring.io/guides/gs/accessing-data-jpa/

I'm using maven, Spring and JPA to create a sample memory based database. I have the following classes;

Model class:

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

@Entity
public class Customer {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    public long getId() { return this.id; }
    public void setId(long Id) { this.id = Id; }

    private String firstName;
    public String getFirstName() { return this.firstName; }
    public void setFirstName(String FirstName) { this.firstName = FirstName; }

    private String lastName;
    public String getLastName() { return this.lastName; }
    public void setLastName(String LastName) { this.lastName = LastName; }

    public Customer(String firstname, String lastname) {
        super();
        this.firstName = firstname;
        this.lastName = lastname;
    }

    public Customer() {
        super();
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}

CustomerRepository Class

import java.util.List;

import org.springframework.data.repository.CrudRepository;

import com.accenture.cursojava.modelos.Customer;

public interface CustomerRepository extends CrudRepository<Customer, Long>{
    List<Customer> findByLastName(String lastName);
}

Application Class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

import com.accenture.cursojava.dataaccess.CustomerRepository;
import com.accenture.cursojava.modelos.Customer;

@Configuration
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        CustomerRepository repository = context.getBean(CustomerRepository.class);
        repository.save(new Customer("Cliente", "de Prueba 1"));
        repository.save(new Customer("Cliente", "de Prueba 2"));
        repository.save(new Customer("Cliente", "de Prueba 3"));
        repository.save(new Customer("Cliente", "de Prueba 4"));
        repository.save(new Customer("Cliente", "de Prueba 1"));
        repository.save(new Customer("Cliente", "de Prueba 2"));
        repository.save(new Customer("Cliente", "de Prueba 3"));
        repository.save(new Customer("Cliente", "de Prueba 4"));

        Iterable<Customer> clientes = repository.findAll();
        for (Customer customer : clientes) {
            System.out.println(customer.getFirstName());
            System.out.println(customer.getLastName());
        }
        Iterable<Customer> clientes1 = repository.findByLastName("de Prueba 1");
        for (Customer customer : clientes1) {
            System.out.println(customer.getFirstName());
            System.out.println(customer.getLastName());
        }
        context.close();
    }
}

After executing the code and following along exactly like the tutorial points out, I get the following output:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapplication.CustomerRepository] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:987)
    at com.myapplication.Application.main(Application.java:18)

Any ideas on what could be the culprit?

Upvotes: 1

Views: 437

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

Spring does not know anything about this bean CustomerRepository. You need to tell Spring it is a managed bean. You can do so by adding the @Repository annotation.

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>{
    List<Customer> findByLastName(String lastName);
}

You may also need to tell Spring where to look for annotated classes (although maybe @EnableAutoConfiguiration makes that unnecessary - I am not familiar with it)

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories()//specify the base package containing your repository interfaces.
public class Application {

}

Upvotes: 1

Related Questions