2014-10-29 130 views
0

我正在尝试在Groovy中为Spring Boot编写一个简单的Spring Data JPA应用程序。我遵循getting started guide并做了一些基本的转换,以使其适用于Groovy和Spring Boot CLI。未使用CommandLineRunner自动创建Spring Data JPA存储库

我正在同春引导CLI(V1.1.8)代码:

spring run app.groovy 

这将导致错误:

NoSuchBeanDefinitionException: No qualifying bean of type [hello.CustomerRepository] is defined 

有没有人有一个想法,为什么仓库不是自动创建?我觉得我必须错过简单的东西。下面是一个包含代码的所有的app.groovy文件:如果你给它实际的类(即,不是.groovy作为脚本)

package hello 

@Grab("spring-boot-starter-data-jpa") 
@Grab("h2") 

import java.util.List 
import javax.persistence.* 
import org.springframework.boot.SpringApplication 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration 
import org.springframework.context.ConfigurableApplicationContext 
import org.springframework.context.annotation.Configuration 
import org.springframework.data.repository.CrudRepository 

@Entity 
class Customer { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    long id 
    String name 

    Customer() {} 
    Customer(String name) { 
     this.name = name 
    } 
} 

interface CustomerRepository extends CrudRepository<Customer, Long> { 
    List<Customer> findByName(String name) 
} 

@Configuration 
@EnableAutoConfiguration 
class Application implements CommandLineRunner { 

    @Autowired 
    ConfigurableApplicationContext context 

    void run(String[] args) { 
     CustomerRepository repository = context.getBean(CustomerRepository.class) 
     repository.save(new Customer("Jack", "Bauer")) 
    } 
} 
+0

你尝试过加入'@ EnableJpaRepositories' – cfrick 2014-10-29 20:26:10

+0

是的,我曾尝试加入@EnableJpaRepositories到应用程序类。它没有改变结果。 – 2014-10-29 20:35:39

回答

1

一个Groovy CLI应用程序只能扫描JPA库。你可以建立一个jar文件并运行,它应该工作:

$ spring jar app.jar app.groovy 
$ java -jar app.jar 
+0

这个修好了,谢谢! – 2014-10-31 21:22:05