【问题标题】:Spring Data JPA repositories not being auto-created with CommandLineRunner未使用 CommandLineRunner 自动创建 Spring Data JPA 存储库
【发布时间】:2014-12-25 17:22:02
【问题描述】:

我正在尝试在 Groovy 中为 Spring Boot 编写一个简单的 Spring Data JPA 应用程序。我遵循getting started guide 并进行了一些基本的转换,使其能够与 Groovy 和 Spring Boot CLI 一起使用。

我正在使用 Spring Boot CLI (v1.1.8) 运行代码:

spring run app.groovy

这会导致错误:

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

有人知道为什么没有自动创建存储库吗?我觉得我一定错过了一些简单的东西。这是包含所有代码的app.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"))
    }
}

【问题讨论】:

  • 你试过加@EnableJpaRepositories
  • 是的,我尝试将@EnableJpaRepositories 添加到Application 类。它没有改变结果。

标签: spring groovy spring-data spring-boot spring-data-jpa


【解决方案1】:

如果您给 Groovy CLI 应用程序提供实际的类(即不是 .groovy 脚本),它只能扫描 JPA 存储库。您可以构建一个 jar 文件并运行它,它应该可以工作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-12
    • 2016-01-08
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2012-06-21
    相关资源
    最近更新 更多