I added spring-core and spring-context as Maven dependencies manually so Maven will download and manage Spring libraries for this project. These two dependencies are minimal for working with the Spring IoC container and using XML-based configuration:
org.springframework:spring-core:6.2.11— core Spring utility classes and support.org.springframework:spring-context:6.2.11— high-level context features (ApplicationContext implementations, bean factory support, event propagation, resource loading).
In pom.xml the relevant sections are placed under <dependencies> so mvn will fetch and place these JARs on the classpath for compilation and runtime.
Note: If you run into compatibility or module issues (Java module system or newer Java versions), ensure your Maven compiler settings coincide with the JDK you're using. In this project the POM sets
maven.compiler.sourceandmaven.compiler.targetto25(update these to your actual JDK version if needed).
This package shows a minimal example of using Spring's IoC container (XML configuration) to manage a simple POJO bean.
Key concepts demonstrated:
-
IoC (Inversion of Control) — instead of the application (your code) instantiating objects directly, the container instantiates and wires them. The container controls the life cycle and configuration of objects.
-
DI (Dependency Injection) — Spring injects values or dependencies into beans. In this example the
MyBean'smessageproperty is injected via XML<property name="message" value="..."/>. -
ApplicationContext — a Spring container interface that loads bean definitions and manages beans.
ClassPathXmlApplicationContextis an implementation that reads an XML file from the classpath. -
Bean definition (XML) —
applicationBeanContext.xmldefines the beanmyBeanand provides property injection formessage. The beanidis the name you use when callingcontext.getBean("myBean"). -
Bean scope — by default beans are singletons in Spring: the container creates one instance per context. For testing or learning you can change to
prototypescope to get multiple instances. -
Type casting —
getBean(String)returnsObject, so you cast to the expected type. Alternatively usegetBean(Class<T>)orgetBean(String, Class<T>)to avoid explicit casts in modern code. -
Lifecycle and closing context — some ApplicationContext implementations (like
ClassPathXmlApplicationContext) need to be closed to release resources. If you hold the reference in a variable of typeApplicationContext, you won't haveclose()available unless you cast toConfigurableApplicationContextor declare the variable asClassPathXmlApplicationContext.
- Maven downloads
spring-coreandspring-context. - At runtime the
Appclass creates aClassPathXmlApplicationContextwithapplicationBeanContext.xml. - The context parses the XML, instantiates bean(s) and sets properties.
AppretrievesmyBeanfrom the context and uses it (prints it and callsshowMessage()if desired).- (Optional) close the context when finished.
Below are the three project files with inline explanatory comments so you can read them later to refresh each concept quickly.
package car.example.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
// NOTE: the original method signature here is `static void main()` in your snippet.
// To run this class from the JVM, the standard entry point should be:
// public static void main(String[] args)
// I kept your signature but added comments explaining behavior below.
static void main() {
// The ApplicationContext is the central interface to access the Spring IoC container.
// ClassPathXmlApplicationContext loads bean definitions from an XML file on the classpath.
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationBeanContext.xml");
// getBean(String) returns Object, so a cast to the expected type is required.
// You can avoid the cast by using getBean("myBean", MyBean.class) in newer code.
MyBean myBean = (MyBean) context.getBean("myBean");
// Here we simply print the bean's toString(). Because MyBean overrides toString(),
// you'll see the message value printed in the object's representation.
System.out.println(myBean);
// Important note: ClassPathXmlApplicationContext is a closeable resource. If you
// create it and keep a reference as ApplicationContext, you can't call close() directly
// (because ApplicationContext doesn't expose close()). To release resources, either:
// - declare the variable as ClassPathXmlApplicationContext and call close(), or
// - cast to ConfigurableApplicationContext and call close(), or
// - use a try-with-resources with ClassPathXmlApplicationContext.
// For small programs this often isn't a problem, but in long-running apps resource leaks
// may occur if context is not closed properly.
}
}package car.example.bean;
public class MyBean {
// simple POJO property that will be injected from XML config
private String message ;
// Setter method is required for Spring's setter-based injection (property injection).
// The XML <property name="message" value="..."/> will call this setter.
public void setMessage(String message) {
this.message = message;
}
// A convenience method demonstrating behavior the bean can provide. In real apps
// beans often encapsulate business logic or coordinate other components.
public void showMessage() {
System.out.println("Message: " + message);
}
// Overriding toString() makes logging and debugging easier because you can
// see internal state when printing the bean reference.
@Override
public String toString() {
return "MyBean{" +
"message='" + message + '\'' +
'}';
}
}<?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">
<!-- Declare beans to be managed by the Spring IoC container. Each <bean> element
creates and configures an instance of the specified class. -->
<!--
id: the name used to look up the bean in the container (context.getBean("myBean"))
class: fully-qualified class name of the bean
By default, scope is "singleton" — Spring will create one shared instance per container.
-->
<bean id="myBean" class="car.example.bean.MyBean">
<!-- property injection: set the "message" property by calling setMessage(...) -->
<property name="message" value="I am a first bean"/>
</bean>
</beans>