简体   繁体   English

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

[英]Grails 3 with Spring boot autoconfigure

I'm trying to integrate a Grails application with the Netflix Eureka stuff for using Spring Cloud Ribbon for making REST calls to services. 我正在尝试将Grails应用程序与Netflix Eureka集成在一起,以使用Spring Cloud Ribbon对服务进行REST调用。 In a normal Spring Boot application it is nothing more than adding the required dependencies and the spring boot autoconfigure will make sure that my RestTemplate is configured for the Ribbon use. 在普通的Spring Boot应用程序中,无非就是添加必需的依赖项,并且Spring Boot自动配置将确保将我的RestTemplate配置为功能区使用。

But in our Grails (3.0.7) application the Spring Boot autoconfiguration will not kick in. Does anyone have an idea to get the Grails with Spring Boot autoconfigure working? 但是在我们的Grails(3.0.7)应用程序中,Spring Boot自动配置将无法启动。是否有人有想法让Spring Boot自动配置正常运行的Grails?

Found the problem. 找到了问题。 Spring boot's @AutoConfigure was working after all. 春季启动的@AutoConfigure在工作。

Problem when trying to use a Spring RestTemplate for rest with Ribbon: 尝试使用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"
    }
}

Because Spring Boot registers the Ribbon enabled RestTemplate bean not under bean name restTemplate , the Grails convention based injection mechanism (field name must match bean name) doesn't work. 因为Spring Boot不在使用bean名称restTemplate下注册了启用了Ribbon的RestTemplate bean,所以基于Grails约定的注入机制(字段名称必须与bean名称匹配)不起作用。 To work around this problem it is needed to @Autowired to the restTemplate field and let Spring do the injection. 要解决此问题,需要@AutowiredrestTemplate字段,并让Spring进行注入。

So this is the solution: 所以这是解决方案:

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