从src / groovy访问Grails服务 [英] Accessing Grails services from src/groovy

查看:149
本文介绍了从src / groovy访问Grails服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails服务是用于在控制器之外实现业务逻辑(以及连接到后台服务/ DB等)的抽象。因此,在一个典型的控制器中,您可能会有:

$ pre code class DashboardController {
StatisticsService statsService

def index(){
//获取需要显示到仪表板上的
// admin的所有统计信息。
AdminDashboardMetrics adm = statsService.getAdminStats()

render(view:/ dashboard,model:[adm:adm])
}
}

这里,Grails自动将 DashboardController 注入一个bean实例 StatisticsService (当然提供的服务可以用 grails create-service ... 来正确创建) p>

但是当我需要访问控制器的 StatisticsService 时会发生什么情况,特别是,根据 src / groovy

  / / src / groovy / com / example / me / myapp / FizzBu​​zzer.groovy 
class FizzBu​​zzer {
StatisticsService statsService

FizzBu​​zzer(StatisticsService statsService){
super()

this.statsService = statsService
}

def doSomething(MyData input){
MoreData result = statsService.calculate(inp ut)

//以某种方式使用'result',等等......
}
}

如何正确注入 FizzBu​​zzer StatisticsService > instance ad传递给 DashboardController

解决方案

你可以在spring bean中通过在resources.groovy中定义注入登录来注入grails服务,在conf> spring



当我在src / groovy中创建一个ExampleService和Example类时, p>

ExampleService

  class ExampleService {

def serviceMethod(){
println做某事
}

}

src / groovy下的示例类

  class示例{

ExampleService exampleService
$ b $ def def doSomething(){
def result = exampleService.serviceMethod()
}
}

resources.groov y在conf下> spring

  beans = {

ex(例子){bean - >
exampleService = ref('exampleService')
}
}

所以我可以在grails-app中定义 Example 作为spring bean,它将自己注入ExampleService。



希望这有帮助。谢谢

Grails services are abstractions used for implementing business logic (as well as connecting to backing services/DBs, etc.) outside of a controller. So in a typical controller you might have:

class DashboardController {
    StatisticsService statsService

    def index() {
        // Fetches all the stats that need to be displayed to the
        // admin on the dashboard.
        AdminDashboardMetrics adm = statsService.getAdminStats()

        render(view: "/dashboard", model: [ adm: adm ])
    }
}

Here, Grails automatically injects the DashboardController with a bean instance of StatisticsService (provided of course that the service was properly created with grails create-service ...).

But what happens when I need to access StatisticsService outside of a controller, and particularly, under src/groovy?

// src/groovy/com/example/me/myapp/FizzBuzzer.groovy
class FizzBuzzer {
    StatisticsService statsService

    FizzBuzzer(StatisticsService statsService) {
        super()

        this.statsService = statsService
    }

    def doSomething(MyData input) {
        MoreData result = statsService.calculate(input)

        // use 'result' somehow, etc....
    }
}

How do I properly inject FizzBuzzer with the same StatisticsService instance ad what is passed into DashboardController?

解决方案

You can inject grails service in spring bean by defining injection login in resources.groovy under conf > spring

As I made an ExampleService and Example class in src/groovy

ExampleService

class ExampleService {

 def serviceMethod() {
    println "do something"
 }

}

Example Class under src/groovy

class Example {

ExampleService exampleService

 def doSomething() {
    def result = exampleService.serviceMethod()
 }
}

resources.groovy under conf>spring

beans = {

 ex(Example){ bean ->
    exampleService = ref('exampleService')
 }
}

so I can define Example ex as spring bean in grails-app and it will have ExampleService injected by itself.

Hope this helps. Thanks

这篇关于从src / groovy访问Grails服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆