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'
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
- Resolving DNS_PROBE_FINISHED_NXDOMAIN Google Chrome Error - Google
- What does b prefix before a String mean in Python? - Python
- [Mac] Find a file using filename in macOS Terminal - MacOS
- Calculate Volume of Cylinder - C-Program
- Enable Cloud Based Clipboard for Images and Text on Windows 10/11 - Windows
- Send Email with attachment using SharePoint PowerShell, SMTP server - SharePoint
- Search text in Eclipse Console logs - Eclipse
- 3 Ways and Steps to Start Windows 11 PC in Safe Mode - Windows-11