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'
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- How to turn off Stage Manager - macOS Ventura - MacOS
- How to Get Substring from a String in Python using string slicing - Python
- How to read int value using Scanner Class Java - Java
- How to know total number of lines in a File using Java. - Java
- That did'nt work, Issue type User not in directory - SharePoint external access error - SharePoint
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program - Powershell
- Location of eclipse.ini file on Mac OS X - Mac-OS-X
- Read a file using Java 8 Stream - Java