Spring Boot - 找不到所需的bean - 但是接口是在同一个类中定义的

Ste*_*fan 2 java spring

在运行时尝试运行以下示例Spring启动代码时,我收到错误

springbootdemo.SpringbootDemoApplication中方法运行器的参数0需要一个无法找到的类型为"springbootdemo.SpringbootDemoApplication $ ReservationRepository"的bean.

这是我的示例代码:

@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;                                                                                               
        }                                                                                                               
    }                                                                                                                   
}    
Run Code Online (Sandbox Code Playgroud)

由于接口存在,我无法弄清楚导致该问题的原因,不是吗?

我正在使用start.spring.io的一个新的启动器发布1.4.7.RELEASE

Bog*_*ros 7

默认情况下,Spring不解析内部存储库,因此您需要显式启用此功能.
只需@EnableJpaRepositories在您的配置类(或Application starter类,它也是您的案例中的配置)上使用该标志considerNestedRepositories = true.

@SpringBootApplication      
@EnableJpaRepositories(considerNestedRepositories = true)                                                                                            
public class SpringbootDemoApplication
Run Code Online (Sandbox Code Playgroud)

EnableJpaRepository doc.