gpt4 book ai didi

grails - 将 Java 类注入(inject) Grails Groovy 服务

转载 作者:行者123 更新时间:2023-12-02 06:39:43 30 4
gpt4 key购买 nike

将 Groovy 服务注入(inject)其他 Groovy 服务或 Controller 非常容易。但是,我不知道如何将 Java 类注入(inject)到 groovy 服务中。

是否可以执行这样的依赖注入(inject)?

最佳答案

是的。您所要做的就是在 resources.groovy 中定义您的 bean,例如

beans = {
//....
myJavaComponent(com.example.YourJavaComponent) {
//if it has any external dependencies you can define them as follow:
externalDependency = ref('idOfTheBeanToInject')
}

然后您所要做的就是通过在类中定义一个属性,将此 bean 注入(inject)到您的 Grails 服务中:def myJavaComponent,仅此而已。

参见Using the Spring Bean DSL in the Grails docs

关于grails - 将 Java 类注入(inject) Grails Groovy 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418764/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com