When creating a grails service many tutorials show something like: grails create-service myproject.DoStuff but it would seem that grails create-service myproject.doStuff works just as well, and they both make a service called “DoStuffService” Using all upper case seems to work fine, but there seem to be lots of places that show it with a lower case first letter. I submit this example of lower case (the first code snip and def authorService about halfway down the page): http://grails.org/doc/2.1.0/guide/services.html
Which way is preferred? Why? If you create the service with lower case in the grails command do you need to inject it with lower case def doStuff or should def DoStuff work either way?
Both work, but I think it’s more common to lowercase the first name since conceptually you’re creating a “person service” with
grails create-service person. But the convention in Java and Groovy is to upper-case class names, and in Grails to suffix service class names with “Service”, so this results in a class name ofPersonService.The bean name is separate from all this though. The convention for Spring bean names is to lowercase the first letter like variables, so Grails auto-registers PersonService as the
personServicebean, so you should usedef personService.The reason
def personServiceworks is that the Groovy compiler converts non-scoped fields like this to a private field and a public getter and setter method. If the getter or setter method already exists it won’t replace yours though. Grails configures its beans to use autowiring by name, so Spring sees thesetPersonServicemethod and calls it to set its value to thepersonServicebean.