Search code examples
javaspringgrailsgroovy

How does class gets injected which implements interface method being called in Java or Groovy code?


I have below working groovy code from Spring framework project:

import org.springframework.oxm.Unmarshaller

public class ItemSearchService  {
    Unmarshaller unmarshaller;
    public ItemSearchResponse getObject(InputStream xml) {
    
        ItemSearchResponse its = null;
        try {
            its = (ItemSearchResponse) unmarshaller.unmarshal(new StreamSource(xml));
        } finally {
        }
        return its;
    }
}

Unmarshaller.unmarshall is actually interface method: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/oxm/Unmarshaller.html

Unmarshaller interface is implemented by several classes.

Who decides which implementing class to inject during runtime and how it decides which class to use?

Does Spring IoC mechanism does this? If so does it pick up one specific implementing class during build time when jar is generated or it does it during run time?

Also how to know which implementing class it actually used?

Will above code work outside of Spring in ordinary Java file assuming dependent jars in classpath?


Solution

  • As soon as it goes about a Grails project (not a pure Spring one), some things should be clarified.

    Who decides which implementing class to inject during runtime and how it decides which class to use? Does Spring IoC mechanism does this? If so does it pick up one specific implementing class during build time when jar is generated or it does it during run time?

    What Spring does in a plain Spring(Boot) is nicely described by @svr s answer and other documentation on the Internet, but has not much to do with your case.

    In Grails the convention-over-configuration doctrine is used, meaning that

    • by default Bean Autowiring is active
    • only beans which are declared as Grails artefacts, like Service are auto-injected.

    Usually, if you want to autowire a bean of other stereotype, you have to declare it somewhere, otherwise Spring IoC container inside Grails simply won't find it.

    You shall check your grails-app/conf/spring folder for resources.groovy (or maybe yaml or xml files depending on version ) and see if your unmarshaller bean is defined there. It should look like:

    beans = {
      unmarshaller Unmarshaller ....
    }
    

    So, basically it doesn't matter how many implementations of Unmarshaller interface are present in all project's jar files. The only thing what matters is what defined in your resources.groovy.

    If you want to check the class of the bean in runtime, just log it's class in your service:

    class ItemSearchService  {
      Unmarshaller unmarshaller
      ItemSearchResponse getObject(InputStream xml) {
         log.info "unmarshaller's class is $unmarshaller.class"
         // or
         // println "unmarshaller's class is $unmarshaller.class"
      }
    }