In this blog post, we will explore the Spring IoC container, its configuration, usage, and examples. We'll cover the following topics:
The Spring container is a core component of the Spring Framework. It is responsible for creating and managing the lifecycle and configuration of application objects (beans). The container reads configuration metadata to know how to instantiate, configure, and assemble these beans. This metadata can be supplied in various forms, such as XML, annotations, or Java code.
Spring provides two main types of containers:
The org.springframework.beans and org.springframework.context packages are fundamental to the Spring Frameworkâs IoC container.
Configuration metadata is how you instruct the Spring container about the objects it should manage. It defines how beans are created, configured, and assembled within the Spring IoC container. There are three primary ways to provide configuration metadata to the Spring container:
Spring provides various implementations of the ApplicationContext interface, each suited for different scenarios. Here are the common ones:
ClassPathXmlApplicationContext
, but the
XML configuration file can be loaded from anywhere in the file system.Using XML Configuration:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Using Java Configuration:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Both BeanFactory
and ApplicationContext
provide the getBean()
method
to retrieve beans from the Spring container.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
Add the necessary Spring dependencies to your 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
<groupId>net.javaguides.spring
<artifactId>spring-ioc-example
<version>0.0.1-SNAPSHOT
<properties>
<maven.compiler.target>17
<maven.compiler.source>17
</properties>
<dependencies>
<dependency>
<groupId>org.springframework
<artifactId>spring-context
<version>6.0.6
<dependency>
<dependencies>
<project>
Create an applicationContext.xml
file in the src/main/resources directory:
<?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="greetingService" class="net.javaguides.spring.ioc.GreetingService">
<property name="message" value="Hello, Spring XML Configuration!"/>
</bean>
</beans>
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");
GreetingService greetingService = (GreetingService) context.getBean("greetingService");
greetingService.getMessage();
}
}
package net.javaguides.spring.ioc;
public class GreetingService {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Message: " + message);
}
}
Use the same Maven project setup as the XML example.
Create an AppConfig.java file:
package net.javaguides.spring.ioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public GreetingService greetingService() {
GreetingService greetingService = new GreetingService();
greetingService.setMessage("Hello, Spring Java Configuration!");
return greetingService;
}
}
package net.javaguides.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
GreetingService greetingService = (GreetingService) context.getBean("greetingService");
greetingService.getMessage();
((AnnotationConfigApplicationContext) context).close();
}
}
package net.javaguides.spring.ioc;
public class GreetingService {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Message: " + message);
}
}
In this blog post, we covered the basics of the Spring IoC container, including how it works, what configuration metadata is, and how to create and retrieve beans from the container using both XML and Java-based configurations. This flexibility allows you to choose the most suitable configuration method for your project's needs. By understanding these concepts, you can effectively leverage the power of Spring to manage your application's dependencies and configurations.