【问题标题】:Grails 3 with Spring boot autoconfigure带有 Spring Boot 自动配置的 Grails 3
【发布时间】:2016-03-14 18:59:09
【问题描述】:

我正在尝试将 Grails 应用程序与 Netflix Eureka 东西集成,以便使用 Spring Cloud Ribbon 对服务进行 REST 调用。在普通的 Spring Boot 应用程序中,它只不过是添加所需的依赖项,而 Spring Boot 自动配置将确保我的 RestTemplate 配置为供 Ribbon 使用。

但是在我们的 Grails (3.0.7) 应用程序中,Spring Boot 自动配置不会启动。有人知道让 Grails 与 Spring Boot 自动配置一起工作吗?

【问题讨论】:

  • 要么在 resource.groovy 中设置你的东西,要么在你的应用程序中添加一个 @ComponentScan("packagename")

标签: grails spring-boot


【解决方案1】:

发现问题。 Spring boot 的 @AutoConfigure 确实有效。

尝试将 Spring RestTemplate 与 Ribbon 一起使用时出现问题:

class MyController {

    RestTemplate restTemplate

    def index() {
        def result = restTemplate.getEntity("http://my-service/whatever", Void.class) // call gives nullPointerException due restTemplate is not injected
        render "Response: $result"
    }
}

因为 Spring Boot 注册的 Ribbon enabled RestTemplate bean 不在 bean 名称 restTemplate 下,所以基于 Grails 约定的注入机制(字段名称必须与 bean 名称匹配)不起作用。要解决这个问题,需要@AutowiredrestTemplate 字段并让Spring 进行注入。

所以这是解决方案:

class MyController {

    @AutoWired
    RestTemplate restTemplate

    def index() {
        def result = restTemplate.getEntity("http://my-service/whatever", Void.class) // restTemplate is now injected using Spring instead of Grails
        render "Response: $result"
    }
}

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 2017-12-11
    • 2021-12-05
    • 1970-01-01
    • 2015-09-11
    • 2021-06-10
    • 2017-01-19
    • 2018-09-09
    • 2018-09-06
    相关资源
    最近更新 更多