To define an application context in Spring using ClassPathXmlApplicationContext
or AnnotationConfigApplicationContext
, you establish the context for your Spring-managed beans using XML configuration in the former, and Java-based annotations in the latter. Here’s how each can be used:
1. Using ClassPathXmlApplicationContext
This approach loads the application context configuration from an XML file.
Example:
package org.kodejava.spring;
import org.kodejava.spring.MyBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlAppContextExample {
public static void main(String[] args) {
// Load application context from XML configuration file
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// Retrieve a bean from the context
MyBean myBean = context.getBean(MyBean.class);
System.out.println(myBean.sayHello());
}
}
Key Points:
- The XML file (
applicationContext.xml
) must be placed in the classpath. - Define your beans and their dependencies inside the XML file.
Example applicationContext.xml
:
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Define a simple bean -->
<bean id="myBean" class="org.kodejava.spring.MyBean"/>
</beans>
2. Using AnnotationConfigApplicationContext
This approach leverages Java-based configuration and annotations to define the context and beans.
Example:
package org.kodejava.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AnnotationAppContextExample {
public static void main(String[] args) {
// Load application context from a Java configuration class
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Retrieve a bean from the context
MyBean myBean = context.getBean(MyBean.class);
System.out.println(myBean.sayHello());
}
}
Java Configuration Class:
package org.kodejava.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "org.kodejava.spring") // Automatically detect beans in this package
public class AppConfig {
// Optionally define beans manually if needed
@Bean
public MyBean myBean() {
return new MyBean();
}
}
Example Component Class:
package org.kodejava.spring;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
public String sayHello() {
return "Hello from MyBean!";
}
}
Summary of Each Approach:
Feature | ClassPathXmlApplicationContext | AnnotationConfigApplicationContext |
---|---|---|
Configuration Style | XML-based | Java-based annotations |
Setup Effort | Requires maintaining XML files separately | Use annotations and Java configuration classes |
Readability | Can become verbose as the application grows | Clean and concise, readable for Java developers |
Dependency Injection | Declared in XML | Defined via @Component , @Bean , @Autowired , etc. |
Preferred Use Case | Legacy or existing applications | Modern, annotation-based Spring applications |
For modern Spring applications, AnnotationConfigApplicationContext
(annotation-based configuration) is the recommended approach due to its ease of use, better readability, and alignment with modern Spring best practices.
Maven Dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.6</version>
</dependency>