Skip to content

Spring Inversion Of Control XML#

Development Process With XML#

  • As we known in Spring Inversion Of Control, there are 3 ways that we can use to configure the Spring Container in Spring Framework:

    • XML configuration file (legacy, but most legacy apps still use this)
    • Java Annotations (modern)
    • Java Source Code (modern)
  • So for configuring Spring Container using XML configuration file, we should follows these step below:

    • Configure Spring Beans.
    • Create a Spring Container.
    • Retrieve beans from Spring Container for using.

Configure The Spring Bean XML#

  • The Spring Bean will be configured in an XML configuration file and in this file we will configure the tag bean with id and class.
  • Let's see the example below with the XML configuration file applicationContext.xml. For the tag bean, the id is like an alias, and class is the fully qualified class name of implementation java class
applicationContext.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="englishCoach" class="com.spring.core.spring.inversion.of.control.EnglishCoach">

    </bean>

</beans>

Create A Spring Container#

  • Spring Container is generally known as ApplicationContext and we have some implementation of it as below:
  • Specialized implementations of ApplicationContext:

    • ClassPathXmlApplicationContext
    • AnnotationConfigApplicationContext
    • GenericWebApplicationContext
    • other...
  • For ClassPathXmlApplicationContext: we can create Spring Container by creating new ClassPathXmlApplicationContext with the name of config file (applicationContext.xml) as below.

1
2
// create a spring container
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Retrieve Beans From Container#

  • Now, to get the bean from the container we will use the method getBean(<beanId>, <interface class>). In which the beanId is the id of bean that we defined in applicationContext.xml and interface class is the interface of implemented class that we also defined in applicationContext.xml.
1
2
3
4
5
// create a spring container
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//retrieve bean from spring container
EnglishCoach englishCoach = context.getBean("englishCoach", Coach.class);
  • Why do we specify the interface in getBean()?
    • When we pass the interface to the method, behind the scenes Spring will cast the object for us.
    • However, there are some slight differences than normal casting.
    • From the Spring docs: Behaves the same as getBean(String), but provides a measure of type safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).

Example Code With XML#

Dependencies#

  • To begin with the example, we will need to use the dependency spring-context for creating the Spring Container.
pom.xml
1
2
3
4
5
6
7
8
9
....  

<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>5.3.24</version>  
</dependency>

....

Configure The Spring Bean#

  • Let's create an interface and implementation class as below.
Coach.java
1
2
3
4
5
6
7
package com.spring.core.spring.inversion.of.control;  

public interface Coach {  

    public String getDailyHomeWork();  

}
EnglishCoach.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.spring.core.spring.inversion.of.control;

public class EnglishCoach implements Coach {

    @Override
    public String getDailyHomeWork() {
        return "Spend 1 hour to practise Speaking Skill!";
    }

}
  • Then in the resources let's create an applicationContext.xml for configuring Spring beans.
applicationContext.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="englishCoach" class="com.spring.core.spring.inversion.of.control.EnglishCoach">

    </bean>


</beans>

Create A Spring Container#

  • Now, let's use ClassPathXmlApplicationContext for creating the applicationContext which is known as Spring Container as below:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package com.spring.core.spring.inversion.of.control;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {

    public static void main(String[] args) {

        //Load Spring Configuration File
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    }

}
  • In which the ClassPathXmlApplicationContext will be created with applicationContext.xml file.

Retrieve Beans From Container#

  • Now, we can use the bean in the Spring Container by using getBean() method.
SpringApplication.java
 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.spring.core.spring.inversion.of.control;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {

    public static void main(String[] args) {

        //Load Spring Configuration File
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //retrieve bean from the spring container
        Coach englishCoach = context.getBean("englishCoach", Coach.class);

        //use the bean
        System.out.println(englishCoach.getDailyHomeWork());

        //close the context
        context.close();

    }

}

Testing#

  • Finally, we just simply run the main class then we can see the content in the spring bean as below.
1
2
3
Spend 1 hour to practise Speaking Skill!

Process finished with exit code 0

See Also#

References#