ClassPathXmlApplicationContext
时间: 2024-05-12 10:21:04 浏览: 71
ClassPathXmlApplicationContext is a class in the Spring Framework that provides an implementation of the ApplicationContext interface. It is used to load and initialize a Spring container by reading the configuration metadata from an XML file located in the classpath of the application.
The ClassPathXmlApplicationContext is typically used in standalone applications, where the configuration file is bundled with the application as a resource. It allows for easy configuration of the Spring container and provides a simple way to bootstrap the application.
To use the ClassPathXmlApplicationContext, you need to create an instance of the class and pass the location of the XML configuration file as a constructor argument. Once the container is created, you can retrieve beans from it using the getBean method.
Example:
```
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService myService = (MyService) context.getBean("myService");
myService.doSomething();
```
In this example, the applicationContext.xml file is located in the classpath of the application, and the myService bean is retrieved from the Spring container using the getBean method.
阅读全文