Reputation: 2613
When trying to run the following sample spring boot code during runtime I get the error
Parameter 0 of method runner in springbootdemo.SpringbootDemoApplication required a bean of type 'springbootdemo.SpringbootDemoApplication$ReservationRepository' that could not be found.
This is my sample code:
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
@Bean
CommandLineRunner runner(ReservationRepository rr) {
return strings -> {
Arrays.asList("Les, Josh, Phil, Sasha, Peter".split(","))
.forEach(n -> rr.save(new Reservation(n)));
rr.findAll().forEach(System.out::println);
rr.findByReservationName("Les").forEach(System.out::println);
};
}
interface ReservationRepository extends JpaRepository<Reservation, Long> {
// select * from reservation where reservation_name = :rn
Collection<Reservation> findByReservationName(String rn);
}
@Entity
class Reservation {
@Id
@GeneratedValue
private Long id;
private String reservationName;
public Reservation() { // for JPA - god sake why :-(
}
public Reservation(String reservationName) {
this.reservationName = reservationName;
}
@Override
public String toString() {
return "Reservations{" +
"id=" + id +
", reservationName='" + reservationName + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
}
I can't figure out what is causing that problem as the interface is present, isn't it?
I am using a fresh starter from start.spring.io for release 1.4.7.RELEASE
Upvotes: 2
Views: 1103
Reputation: 1299
By default, Spring is not parsing inner repositories, so you need to explicitly enable this functionality.
Just use @EnableJpaRepositories
on your configuration class (or Application starter class, it is also a configuration on your case) with the flag considerNestedRepositories = true
.
@SpringBootApplication
@EnableJpaRepositories(considerNestedRepositories = true)
public class SpringbootDemoApplication
EnableJpaRepository doc.
Upvotes: 7