简体   繁体   中英

Spring Boot - Required bean not found - but interface is defined in same class

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM