🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In a previous article, we discussed What is Spring IOC Container and how it works. Now, let's dive into a practical example to understand how to configure Spring beans using XML-based configuration metadata.
Ways to Supply Configuration Metadata to the Spring IoC Container
Spring IOC Container XML Config Example
In this example, we will supply XML-based configuration metadata to the Spring IoC container.
Development Steps
Follow these steps to develop a Spring application:
- Create a simple Maven Project
- Add Maven Dependencies
- Configure HelloWorld Spring Beans
- Create a Spring Container
- Retrieve Beans from the Spring Container
Tools and Technologies Used
- Spring Framework - 6.0.6
- JDK - 17 or later
- Maven - 3.2+
- IDE - Eclipse Mars/STS
Step 1: Create a Simple Maven Project
Create a simple Maven project using your favourite IDE and refer to the Guide to Create a Simple Maven Project.
Step 2: Project Structure
The below diagram shows a project structure for your reference:
Step 3: Add Maven Dependencies
Add the following content to the pom.xml file:
<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>net.javaguides.spring</groupId>
<artifactId>spring-ioc-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
</project>
Step 4: Configure HelloWorld Spring Beans
What is a Spring Bean?
A Spring bean is a Java object that is managed by the Spring container.
Create a HelloWorld Java class with the following content:
package net.javaguides.spring.ioc;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("My Message : " + message);
}
}
Next, configure the HelloWorld class as a Spring bean using XML-based configuration:
<?xml version = "1.0" encoding = "UTF-8"?>
<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="helloWorld" class="net.javaguides.spring.ioc.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>
Step 5: Create a Spring Container
If you have a Spring bean configuration XML file in a standalone application, you can use ClassPathXmlApplicationContext class to load the file and get the container object.
package net.javaguides.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
Step 6: Retrieve Beans from the Spring Container
The ApplicationContext interface provides the getBean() method to retrieve the bean from the Spring container.
package net.javaguides.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Output
My Message : Hello World!
Conclusion
In this tutorial, we learned how to configure spring beans using XML-based configuration. We covered the steps to create a Maven project, add dependencies, configure beans, create a Spring container, and retrieve beans from the container.
The source code for this example is available in my GitHub repository.
Additional Resources
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment