Skip to content

Latest commit

 

History

History
150 lines (109 loc) · 6.18 KB

01_InversionOfControl.adoc

File metadata and controls

150 lines (109 loc) · 6.18 KB

Inversion of Control in Spring

Spring - Inversion of Control

Understanding how Spring "loads" its "beans".

As mentioned in the introduction, Inversion of Control and Dependency Injection bring about a flexibility to programming.

Let us walk through a few examples that show how Inversion of Control is possible with Spring.

Java Example without Spring

  1. We use a model
    ColoredShape.java.

  2. A plain Java example with no spring used
    Ex00_NoFrameworkExample.java.

  3. In order to produce a different color and shape, either, the command line arguments or, the java code needs to be manipulated.

Output
Running the main method should output
> pink polygon
created with default values on ColoredShape which itself is instantiated with a default constructor.
Questions
  1. What if the requirement was to produce two shapes: a pink and a cyan triangle?

  2. What if the requirement was to include a blue square in addition?



Using Spring to achieve Inversion of Control (IoC)

Example 1 - Using Spring with Constructor Instantiation

  1. For the model, we have two model objects to look at
    ColoredShape.java

  2. Here is an example of using Spring with a constructor instantiating the value
    Ex01_InstantiationThroughConstructor.java
    This class has a ClassPathXmlApplicationContext which reads the XML configuration. The ApplicationContext is the Spring Inversion of Control. The main method transfers control of loading beans to the ClassPathXmlApplicationContext, which then loads beans from the XML file.

  3. Here is the XML configuration for this Spring class
    ex01-instantiation-through-constructor.xml.

  4. Notice how the default constructor from ColoredShape is used to configure the properties.

Output
Running the main method should output
> green circle
created with default values on ColoredShape which itself is instantiated with a default constructor.

Example 2 - Using Spring with Static Factory Instantiation

  1. Here is an example of using Spring with a static factory instantiating the value
    Ex02_InstantiationThroughStaticFactory.
    A static method, getInstance() is a supplier of a ColoredShape instance.

  2. Here is the configuration for this Spring class
    ex02-instantiation-through-static-factory.xml.
    The bean is declared with a factory-method, which points to the getInstance() method.

  3. Notice how the static factory method from Ex02_InstantiationThroughStaticFactory is used to configure the properties.

Output
Running the main method should output
> harlequin hexagon
created due to the invocation of the static factory method on Ex02_InstantiationThroughStaticFactory

Example 3 - Using Spring with Instance Factory Instantiation

  1. Here is an example of using Spring with an instance factory instantiating the value
    Ex03_InstantiationThroughInstanceFactory.java.
    An instance method getInstance() is a supplier of the ColoredShape instance. The constructor is first called to create the class, the ClassPathXmlApplicationContext then parses the XML to configure the bean.

  2. Here is the configuration for this Spring class
    ex03-instantiation-through-instance-factory.xml.
    The bean is declared with a factory-bean which determines that there is a bean that has an instance factory-method.

  3. Notice how the instance factory method from Ex03_InstantiationThroughInstanceFactory is used to configure the properties. The Ex03_InstantiationThroughInstanceFactory class is first constructed via a default constructor, then the instance factory method is invoked to configure the bean. A simple System.out.println() was added to show the loading of the class.

Output
This should output two lines:
> Ex03_InstantiationThroughInstanceFactory is now loaded.
> red rhombus
created due to the instantiation and invocation of the instance method on Ex03_InstantiationThroughInstanceFactory

Exercise Lab

Lab

The lab exercise is to fix the broken tests. Follow the instructions to fix the TODOs to get the JUnit test to pass.


Prev TOC Next

Introduction

TOC

Dependency Injection