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

Spring IoC container

Module 5. Spring
Level 1 , Lesson 1
Available

This chapter examines the Spring Framework's implementation of the Inversion of Control (IoC) principle. IoC is also known as Dependency Injection (DI). This is the process by which objects define their dependencies (that is, the other objects they operate on) only through constructor arguments, factory method arguments, or properties that are set on an instance of the object after it is created or returned by a factory method. The container then injects these dependencies when the bean is created. This process is essentially an inversion (hence the name, Inversion of Control) of the bean itself, which independently controls the creation or placement of its dependencies using direct class construction or a mechanism such as a Service Locator template.

The org.springframework.beans and org.springframework.context packages are the basis for the Spring Framework IoC container. Interface BeanFactory provides an advanced configuration mechanism capable of managing objects of any type. ApplicationContext is a subinterface of BeanFactory. It adds:

  • Easier integration with AOP features in Spring

  • Processing message resources (for use in internationalization)

  • Publishing events

  • Application-tier specific contexts, such as WebApplicationContext for use in web applications.

In short, BeanFactory provides the core configuration and core functionality, while ApplicationContext adds more enterprise-specific functionality. ApplicationContext is a complete superset of BeanFactory and is used exclusively in this chapter when describing the Spring IoC container. For more information on using BeanFactory instead of ApplicationContext, see the section on BeanFactory API.

In Spring, the objects that form the core of your application and are managed by the Spring IoC container are called beans. A bean is an object that is created, compiled, and managed by the Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans and the dependencies between them are reflected in the configuration metadata used by the container.

Briefly about containers

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and composing beans. The container receives instructions about what objects to create, configure, and link by reading configuration metadata. Configuration metadata is in the form of XML, Java annotations, or Java code. This allows you to express the objects that make up an application and the rich interdependencies between those objects.

Several implementations of the ApplicationContext interface come with Spring. Standalone applications typically create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. Although XML is the traditional format for defining configuration metadata, you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats.

In most use cases, explicit custom code is not required to create one or more instances of a Spring IoC container. For example, in a web application scenario, eight (or so) lines of XML web descriptor template in the application's web.xml file are usually sufficient. If you are using Spring Tools for Eclipse (an Eclipse-based development environment), you can easily create this standard configuration with a few clicks or keystrokes.

The diagram below shows a high-level view of how Spring works. Application classes are combined with configuration metadata so that once the ApplicationContext is created and initialized, you have a fully configured and executable system or application.

Configuration metadata

As shown in the previous diagram, the Spring IoC container uses a form of configuration metadata. This configuration metadata is how you, the application developer, tell the Spring container how to create, configure, and layout objects in your application.

Configuration metadata is traditionally presented in a simple and intuitive XML format, which is what much of this chapter uses to explain key concepts and capabilities of the Spring IoC container.

XML-based metadata is not the only valid form of configuration metadata. The Spring IoC container itself is completely separate from the format in which the configuration metadata is actually written. These days, many developers are opting for Java-based configuration for their Spring applications.

For information on using other forms of metadata with a Spring container, see:

  • Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.: Spring 2.5 introduced support for annotation-based configuration metadata.

  • Java-based configuration: As of Spring 3.0, many of the features provided by the Spring JavaConfig project have become part of the core Spring Framework. This way, you can define beans that are external to your application's classes using Java rather than XML files. To use these new features, please refer to the annotations @Configuration, @Bean, @Import and @DependsOn.

A Spring configuration consists of at least one, but usually more than one, bean definition that the container must manage. XML-based configuration metadata configures these beans as <bean/> elements within the top-level <bean/> element. Java configuration typically uses @Bean methods in the @Configuration class.

These bean definitions correspond to the actual objects that make up your application. Typically you define service layer objects, data access objects (DAOs), view objects such as Struts Action instances, infrastructure objects such as Hibernate SessionFactories, JMS Queues and so on. Generally, fine-grained domain objects are not configured in a container because the DAO and business logic are typically responsible for creating and loading domain objects. However, you can use Spring's integration with AspectJ to configure objects that were created outside the control of the IoC container.

The following example shows the basic structure of XML-based configuration metadata:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean are found here -->
    </bean>
    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean are found here -->
    </bean>
    <!-- see more definitions of beans here -->
</beans>
  1. The id attribute is a string that identifies the definition of an individual bean.
  2. The class attribute specifies the type of the bean and uses the fully qualified class name.

The value of the id attribute refers to the interacting objects. The XML for referencing interacting objects is not shown in this example.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION