API / Apache Grails Interview questions
Explain how Grails' dependency injection works with Spring beans?
Grails doesn't build its own separate dependency injection container — it uses Spring's IoC container directly, and much of what looks like "Grails magic" for wiring services, beans, and configuration is really Grails automatically registering conventionally-named artifacts as Spring beans on an application's behalf.
A class ending in Service in grails-app/services, for example, is automatically detected and registered as a Spring bean with a name derived from the class, without a developer writing an explicit @Component annotation or XML bean definition. Any other class — a controller, another service — simply declares a field whose name matches the bean (by convention, the un-capitalized class name), and Spring's normal autowiring-by-name mechanism injects the right instance automatically, the same underlying mechanism that powers explicit @Autowired injection in a plain Spring application.
For cases outside this naming convention, or for wiring in third-party beans not managed by Grails' own artifact scanning, developers can still define beans explicitly through a resources.groovy configuration file or Grails' Spring bean-building DSL, which ultimately registers those beans in the exact same underlying Spring ApplicationContext everything else uses.
More Related questions...