Accessing Grails services from src/groovy

前端 未结 3 874
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 08:11

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 y

3条回答
  •  醉梦人生
    2021-01-21 09:01

    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

提交回复
热议问题