Joh*_*ier 1 grails spring-bean
我想使用依赖注入在我的 Grails 应用程序中使用用 Java 编写的服务。在没有注入的情况下用 Java 创建它看起来像这样:
ServiceFactory.newInstance().getElementService()
Run Code Online (Sandbox Code Playgroud)
我想以与为控制器、服务和作业注入服务的方式相同的方式使用它。
class ImportJob {
def elementService
...
}
Run Code Online (Sandbox Code Playgroud)
我知道这应该进入resources.groovy
,这就是我到目前为止所拥有的:
serviceFactory(ServiceFactory) { bean ->
bean.factoryMethod = 'newInstance'
}
elementService(ElementService) {
}
Run Code Online (Sandbox Code Playgroud)
我在文档中发现很少有资源可以帮助解决这个问题。我如何完成 elementService 以便它如上所述创建对象?我应该使用 BeanBuilder 吗?
您可以FactoryBean
为此创建一个,因为它不像在类中调用方法那么直接:
package com.mycompany
import org.springframework.beans.factory.FactoryBean
import org.springframework.beans.factory.InitializingBean
class ElementServiceFactoryBean implements FactoryBean<ElementService>, InitializingBean {
private ElementService elementService
ElementService getObject() { elementService }
Class<ElementService> getObjectType() { ElementService }
boolean isSingleton() { true }
void afterPropertiesSet() {
elementService = ServiceFactory.newInstance().elementService
}
}
Run Code Online (Sandbox Code Playgroud)
你将它注册resources.groovy
为
elementService(ElementServiceFactoryBean)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1259 次 |
最近记录: |