egg li
egg li

Reputation: 37

Grails-The different dependence Injection ways in grails

What's the difference of different way to inject dependence in grails:

  1. ABCService abcService
  2. def abcService

  3. @Autowired ABCService ABCService

Upvotes: 2

Views: 61

Answers (1)

Igor Artamonov
Igor Artamonov

Reputation: 35961

  1. Expected type is specified, if service with name abcService (or other Spring bean with such name) will have different class, then you'll get ClassCastException here
  2. Just any bean with name abcService
  3. Spring annotation, it's optional. But if you've marked a field but Grails/Spring cannot find such bean it will throw NoSuchBeanDefinitionException (previous two will get null if it doesn't exists) @Autowired could be combined with def type also

Basically Grails services are standard Spring beans, Grails just follows convention over configuration that for every class in services dir it will create a bean with name abcService that could be autowired into other beans. All other job is done by Spring. See also docs for Spring and Grails

Upvotes: 2

Related Questions