CodeGym/Java Course/Module 5. Spring/Using an IoC container

Using an IoC container

Available

ApplicationContext is an interface to an advanced factory capable of maintaining a registry of various beans and their dependencies. Using the T getBean(String name, Class<T> requiredType) method, you can get bean instances.

ApplicationContext allows you to read and access bean definitions, as shown in the following example:

Java
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// get the configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use the configured instance
List<String> userList = service.getUsernameList();
Kotlin
import org.springframework.beans.factory.getBean
// create and configure beans
val context = ClassPathXmlApplicationContext("services.xml", "daos.xml")
// get the configured instance
val service = context.getBean<PetStoreService>("petStore")
// use the configured instance
var userList = service.getUsernameList()

When using a Groovy configuration, bootstrap looks very similar. It has another context implementation class that supports Groovy (but also understands XML bean definitions). The following example shows the Groovy configuration:

Java
ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", "daos.groovy");
Kotlin
val context = GenericGroovyApplicationContext("services.groovy", "daos.groovy")

The most flexible option is GenericApplicationContext in combination with read delegates, such as XmlBeanDefinitionReader for XML files, as shown in the following example:

Java
GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();
Kotlin
val context = GenericApplicationContext()
XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml")
context.refresh()

You can also use GroovyBeanDefinitionReader for Groovy files, as shown in the following example:

Java
GenericApplicationContext context = new GenericApplicationContext();
new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");
context.refresh();
Kotlin
val context = GenericApplicationContext()
GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy")
context.refresh()

You can mix and match such read delegates on the same ApplicationContext by reading bean definitions from different configuration sources.

You can then use getBean to get bean instances. The ApplicationContext interface has several other methods for retrieving beans, but ideally you should never use them in your application code. Indeed, your application code should not have calls to the getBean() method at all, and therefore should not depend on Spring APIs at all. For example, Spring's integration with web frameworks provides dependency injection for various web framework components, such as controllers and managed JSF beans, so that a dependency on a particular bean can be declared using metadata (such as autowiring annotations).

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet