【问题标题】:Grails 2.x service injection in Groovy/srcGroovy/src 中的 Grails 2.x 服务注入
【发布时间】:2012-05-25 07:27:36
【问题描述】:

我想在 Groovy/src 类中注入我的服务。 normaln 依赖注入不起作用:

...
def myService
...

我可以使用它(它有效):

def appCtx = ApplicationHolder.application.getMainContext()
def myService = appCtx.getBean("myService");

但 ApplicationHolder 已被弃用。有没有更好的解决方案?

感谢您的建议

【问题讨论】:

  • 你如何使用这个'src类'?你在哪里以及如何实例化它?
  • 我在其他 Groovy 类中使​​用它(它在那里被实例化)。有一个外观 groovy 类由服务使用,它触发所有这些过程。我不想将使用过的服务作为参数传递,以免传递太多参数...

标签: grails grails-2.0


【解决方案1】:

ApplicationHolder的替换可以是Holders,也可以在静态范围内使用:

import grails.util.Holders
...

def myService = Holders.grailsApplication.mainContext.getBean 'myService'

【讨论】:

    【解决方案2】:

    查看以下 Grails 常见问题解答以从 src/groovy 中的源访问应用程序上下文 - http://grails.org/FAQ#Q:如何从 src/groovy 中的源访问应用程序上下文?

    没有等效于 ApplicationHolder 的 ApplicationContextHolder 类。要从 src/groovy 中的 Groovy 类访问名为 EmailService 的服务类,请使用以下方法访问 Spring bean:

    import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
    import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes as GA
    def ctx = SCH.servletContext.getAttribute(GA.APPLICATION_CONTEXT)
    def emailService = ctx.emailService
    

    【讨论】:

    • 虽然这将允许您访问服务中的方法,但它会破坏服务的事务性。摘自Declarative Transactions警告:依赖注入是声明式事务工作的唯一方式。如果使用new BookService()等new操作符,将不会得到事务性服务
    • @ubiquibacon - 是的,但是“def emailService = ctx.emailService”将是一个交易服务。很抱歉,访问这样的服务有什么问题?
    • 我认为这不符合“依赖注入”的条件。不过,我必须做一些测试来验证这一点。
    • @ubiquibacon 调查结果如何?
    【解决方案3】:

    您可以通过在grails-app/conf/spring/resources.groovy 中配置它们来轻松注册新的(或覆盖现有的)bean:

    // src/groovy/com/example/MyClass.groovy
    class MyClass {
        def myService
        ...
    }
    
    // resources.groovy
    beans = {
        myclass(com.example.MyClass) {
            myService = ref('myService')
        }
    }
    

    你也可以查看这个关于How to access Grails configuration in Grails 2.0?的问题

    【讨论】:

    • 现在我发现这对我不起作用。我不能像这样在 resources.groovy 中绑定服务或其他属性:` import com.path.to.residentAtPlace beans = { residentAtPlaceBean(residentAtPlace) { someProperty = 45 placeService = ref('placeService') grailsApplication = ref('grailsApplication' ) } } ` 在我的 ResidentAtPlace.groovy 中的所有内容都是空的,你看到什么问题了吗?我正在使用 Grail 2.0.3
    • 同样的事情发生在我身上,知道为什么吗?
    【解决方案4】:

    你可以通过resources.groovy

    // src/groovy/com/example/MyClass.groovy
    class MyClass {
        def myService
        ...
    }
    
    // resources.groovy
    beans = {
        myclass(com.example.MyClass) {
            myService = ref('myService')
        }
    }
    

    或者只是使用自动连线注释:

    // src/groovy/com/example/MyClass.groovy
    
    import org.springframework.beans.factory.annotation.Autowired
    
    class MyClass {
        @Autowired 
        def myService
        ...
    }
    
    // resources.groovy
    beans = {
        myclass(com.example.MyClass) {}
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多