簡體   English   中英

具有Spring Boot自動配置功能的Grails 3

[英]Grails 3 with Spring boot autoconfigure

我正在嘗試將Grails應用程序與Netflix Eureka集成在一起,以使用Spring Cloud Ribbon對服務進行REST調用。 在普通的Spring Boot應用程序中,無非就是添加必需的依賴項,並且Spring Boot自動配置將確保將我的RestTemplate配置為功能區使用。

但是在我們的Grails(3.0.7)應用程序中,Spring Boot自動配置將無法啟動。是否有人有想法讓Spring Boot自動配置正常運行的Grails?

找到了問題。 春季啟動的@AutoConfigure在工作。

嘗試使用Spring RestTemplate與功能區RestTemplate休息時出現問題:

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不在使用bean名稱restTemplate下注冊了啟用了Ribbon的RestTemplate bean,所以基於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"
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM