[Spring practice series] (5) Spring application context
The following is the running code of the Spring-Hello-world project:
package com.sjf.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Test class
* @author sjf0115
*
*/
public class Test {
private static ApplicationContext context;
private static HelloWorld helloWorld;
public static void main(String[] args) {
// 1. Create a Spring IOC container
context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. Obtain the Bean instance from the IOC container
helloWorld = (HelloWorld)context.getBean("helloworld");
// 3. Call the sayHello Method
helloWorld.sayHello();
}
}
The Code shows that the first step to use the Spring framework is to use the Spring application context to create a Spring IOC container:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
The following describes the context of the Spring application. Spring comes with several types of application context:
Spring Context |
Description |
ClassPathXmlApplicationContext |
Load the context definition from the XML configuration file in the class path, and use the application context definition file as a class resource. |
FileSystemXmlapplicationcontext |
Read the XML configuration file in the file system and load the context definition. |
XmlWebApplicationContext |
Read the XML configuration file of the Web application and load the context definition. |
We will describe Spring-based Web applications in the future. At that time, we will explain XmlWebApplicationContext in detail. Now we can simply use FileSystemXmlApplicationContext to load the application context from the file system or use ClassPathXmlApplicationContext to load the application context from the class path. The process of loading the Bean to the Bean Factory is similar whether to load the application context from the file system or from the class path. For example, the following code loads a File-SystemXmlApplicationContext:
ApplicationContext context = new FileSystemXmlApplicationContext("d:/applicationContext.xml");
Similarly, you can use ClassPathXmlApplicationContext to load the application context from the application class path:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Use FileSystemXmlApplicationContext and ClassPathXmlApplicationContext
DifferencesIn
- FileSystemXmlApplicationContext searches for the applicationContext. xml file in the specified file system path;
- ClassPathXmlApplicationContext is used to find the applicationContext. xml. xml file under all the class paths (including JAR files.
It indicates two methods for storing applicationContext. xml in the past: I also expressed doubts about the storage location of applicationContext. xml. can I find it by just one name? Now I understand that ClassPathXmlApplicationContext is used to find the applicationContext. xml. xml file in all the class paths (including JAR files), so the above two methods can be found in the Spring IOC container. Through the existing application context reference, you can call the getBean () method of the application context to obtain the Bean from the Spring container.
// 2. Obtain the Bean instance from the IOC container
helloWorld = (HelloWorld)context.getBean("helloworld");