In this tutorial, I will guide all of you on the basic way to initial objects in the Spring container, that is: using the XML file.
I would like to work with the example that we discussed in the tutorial about Dependency Injection.
Here we have an interface named Shape:
1 2 3 4 5 |
package com.huongdanjava.springxmlconfiguration; public interface Shape { public void draw(); } |
An object implements the interface Shape named Circle:
1 2 3 4 5 6 7 |
package com.huongdanjava.springxmlconfiguration; public class Circle implements Shape { public void draw() { System.out.println("Drawing circle ..."); } } |
An object Drawing to draw:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava.springxmlconfiguration; public class Drawing { private Shape shape; public Drawing(Shape shape) { this.shape = shape; } public void setShape(Shape shape) { this.shape = shape; } public void preparing() { System.out.println("Preparing ..."); } public void draw() { shape.draw(); } } |
An XML configuration file for Spring in the folder src/main/resources with the content as below:
1 2 3 4 5 |
<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 http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> |
And below is the Maven project:
To work with the Spring framework, we need to add its dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huongdanjava</groupId> <artifactId>spring-xml-configuration</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.2</version> </dependency> </dependencies> </project> |
OK, now we will create a Circle object first! Because this object does not depend on other objects, so we only need initial this object with an id in the Spring container:
1 2 |
<bean id="shape" class="com.huongdanjava.springxmlconfiguration.Circle"> </bean> |
With the Drawing object, because this object depends on the Circle object which we already created in the Spring container, so we need a way to inject the Circle object into the Drawing object.
As you can see in the code of the Drawing class, we can inject the Circle object into the Drawing object in 2 ways: by a constructor and by setShape() method. In the Spring framework, how can we do that?
First of all, you will declare the Drawing object without dependencies as declaring the Circle object:
1 2 |
<bean id="drawing" class="com.huongdanjava.springxmlconfiguration.Drawing"> </bean> |
And now we will inject the Circle object into the Drawing object by the constructor, called Constructor Injection:
1 2 3 |
<bean id="drawing" class="com.huongdanjava.springxmlconfiguration.Drawing"> <constructor-arg ref="shape" /> </bean> |
Or by setShape() method, called Setter Injection.
In this way, we need to add a default constructor to the Drawing class, because Spring will use this constructor to create the Drawing object, as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava.springxmlconfiguration; public class Drawing { private Shape shape; public Drawing() {} public Drawing(Shape shape) { this.shape = shape; } public void setShape(Shape shape) { this.shape = shape; } public void preparing() { System.out.println("Preparing ..."); } public void draw() { shape.draw(); } } |
Now, you can inject the Circle object using Setter Injection as below:
1 2 3 |
<bean id="drawing" class="com.huongdanjava.springxmlconfiguration.Drawing"> <property name="shape" ref="shape" /> </bean> |
You can choose the way you prefer based on your application.
Let’s run this example.
Here I choose to use Setter Injection, which means the content of XML configuration for Spring should look like as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="drawing" class="com.huongdanjava.springxmlconfiguration.Drawing"> <constructor-arg ref="shape" /> </bean> --> <bean id="drawing" class="com.huongdanjava.springxmlconfiguration.Drawing"> <property name="shape" ref="shape" /> </bean> <bean id="shape" class="com.huongdanjava.springxmlconfiguration.Circle"> </bean> </beans> |
Running example class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.springxmlconfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { // Call Spring container ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // Request to get Drawing object Drawing drawing = (Drawing) context.getBean("drawing"); drawing.draw(); } } |
Result: