It's year 2023 and one may not use the XML based Spring Application Context for new projects in production, but there are few reasons why its still relevant,
- Working on a legacy product: If you are working on a legacy product that uses an older version of Spring or a configuration file written in XML format, you may need to use ClassPathXmlApplicationContext to initialize the Spring application context.
- Learning Spring: If you are new to Spring and want to learn how it works, starting with ClassPathXmlApplicationContext is a good option. It is a simple way to set up a Spring application context using an XML configuration file, and it can help you understand the basic concepts of Spring.
- Limited resources constraint: If you are working on a project with limited resources, using ClassPathXmlApplicationContext is a good option as it has a smaller footprint compared to other Spring container implementations like AnnotationConfigApplicationContext.
- Testing: ClassPathXmlApplicationContext is also commonly used in testing scenarios, where it can be used to create a lightweight Spring context for testing individual components of a larger Spring application.
- Integrating with other frameworks: In some cases, other frameworks may require the use of ClassPathXmlApplicationContext to integrate with Spring. For example, some legacy ORM frameworks may require configuration via an XML file and integration with Spring through ClassPathXmlApplicationContext.
- Limited deployment options: Some cloud deployment platforms may not support certain newer Spring container implementations, in which case ClassPathXmlApplicationContext can be used as a fallback option.
Let us take a example of IoC using a Interface Teacher and its implementations like MathTeacher and ScienceTeacher.
Teacher.javapackage example.spring_ioc_with_xml;
/**
* Our interface to learn
* String IoC using XML
* based ClassPathXmlApplicationContext
*
*/
public interface Teacher {
String doStudies();
}
ScienceTecher.java
package example.spring_ioc_with_xml;
public class ScienceTeacher implements Teacher {
@Override
public String doStudies() {
return "Let us study chapter 'Work & Energy'!";
}
}
MathTecher.java
package example.spring_ioc_with_xml;
public class MathTeacher implements Teacher {
@Override
public String doStudies() {
return "Let us study chapter 'Quadratic Equations'!";
}
}
MusicTeacher.java
package example.spring_ioc_with_xml;
public class MusicTeacher implements Teacher {
@Override
public String doStudies() {
return "Let us learn how to play Guitar!";
}
}
Now lets create our ApplicationContext.xml file.
<?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="mathTeacher" class="example.spring_ioc_with_xml.MathTeacher"/>
<bean id="scienceTeacher" class="example.spring_ioc_with_xml.ScienceTeacher" />
<bean id="musicTeacher" class="example.spring_ioc_with_xml.MusicTeacher" />
</beans>
And its time to create a Main class with the implementation of ClassPathXmlApplicationContext to perform IoC.
package example.spring_ioc_with_xml;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Teacher teacher = classPathXmlApplicationContext.getBean("scienceTeacher", ScienceTeacher.class);
String scienceTeachersMessage = teacher.doStudies();
System.out.println(scienceTeachersMessage);
teacher = classPathXmlApplicationContext.getBean("mathTeacher", MathTeacher.class);
System.out.println(teacher.doStudies());
teacher = classPathXmlApplicationContext.getBean("musicTeacher", MusicTeacher.class);
System.out.println(teacher.doStudies());
}
}
Output:

Note:
Make sure to add the below dependency in your build.gradle dependencies.
implementation group: 'org.springframework', name: 'spring-context', version: '5.3.22'
-
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Microsoft Teams - Where would you like to start - Business or Personal - Teams
- Steps to Integrate Latest Facebook SDK with your Android Application - Facebook
- Limit scrollback rows in macOS Terminal - MacOS
- Docker - Error response from daemon: You cannot remove a running container - Docker
- macOS Big Sur java.lang.UnsatisfiedLinkError CoreFoundation - Android Studio - Android-Studio
- Sort a List using Java 8 Stream Examples - Java
- Create a Gradle Java Project in VS Code - Gradle
- YAML Parser using Java Jackson Library Example - Java