Search code examples
javagrailsgroovy

How to make simple groovy Class behave like a Grails service in groovy module?


Help required. Thanks in advance.

How to make simple groovy Class behave like a Grails service in groovy module? Or How to create Grails service class in Groovy Module??


Solution

  • Basically, a Service class features the following:

    • it is a singleton in the app (by default, other scopes are really rare)
    • it's methods are executed in transactional context by default.
    • it's auto-wired

    so, you can define a Groovy/Java class anywhere, add @Transactional into it, and define it in Spring app-context under conf/spring/resources.groovy as a singleton:

    beans = {
      yourServicePretender YourServicePretenderClassName
    }
    

    Volia, you "service" is ready!