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.
-
We use a model
⇒ ColoredShape.java. -
A plain Java example with no spring used
⇒ Ex00_NoFrameworkExample.java. -
In order to produce a different color and shape, either, the command line arguments or, the java code needs to be manipulated.
Running the main method should output > pink polygon created with default values on ColoredShape which itself is instantiated with a default constructor.
-
What if the requirement was to produce two shapes: a pink and a cyan triangle?
-
What if the requirement was to include a blue square in addition?
-
For the model, we have two model objects to look at
⇒ ColoredShape.java -
Here is an example of using Spring with a constructor instantiating the value
⇒ Ex01_InstantiationThroughConstructor.java
This class has aClassPathXmlApplicationContext
which reads the XML configuration. TheApplicationContext
is the Spring Inversion of Control. Themain
method transfers control of loading beans to theClassPathXmlApplicationContext
, which then loads beans from the XML file. -
Here is the XML configuration for this Spring class
⇒ ex01-instantiation-through-constructor.xml. -
Notice how the default constructor from
ColoredShape
is used to configure the properties.
Running the main method should output > green circle created with default values on ColoredShape which itself is instantiated with a default constructor.
-
Here is an example of using Spring with a static factory instantiating the value
⇒ Ex02_InstantiationThroughStaticFactory.
A static method,getInstance()
is a supplier of aColoredShape
instance. -
Here is the configuration for this Spring class
⇒ ex02-instantiation-through-static-factory.xml.
Thebean
is declared with afactory-method
, which points to thegetInstance()
method. -
Notice how the static factory method from
Ex02_InstantiationThroughStaticFactory
is used to configure the properties.
Running the main method should output > harlequin hexagon created due to the invocation of the static factory method on Ex02_InstantiationThroughStaticFactory
-
Here is an example of using Spring with an instance factory instantiating the value
⇒ Ex03_InstantiationThroughInstanceFactory.java.
An instance methodgetInstance()
is a supplier of theColoredShape
instance. Theconstructor
is first called to create the class, theClassPathXmlApplicationContext
then parses the XML to configure the bean. -
Here is the configuration for this Spring class
⇒ ex03-instantiation-through-instance-factory.xml.
Thebean
is declared with afactory-bean
which determines that there is abean
that has an instancefactory-method
. -
Notice how the instance factory method from
Ex03_InstantiationThroughInstanceFactory
is used to configure the properties. TheEx03_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.
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
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 |
---|---|---|