Here is the code for running the Spring-hello-world project:
package com.sjf.bean;
import org springframework context applicationcontext
import org springframework context support classpathxmlapplicationcontext
/**
* 测试类
* @author sjf0115
*
*/
public class Test {
private static applicationcontext context
private static HelloWorld helloWorld;
public static void main ( string [" args " {
// 1. 创建Spring IOC容器
context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2. Get the bean instance from the IOC container
helloworld = ( helloworld " context getbean ( "HelloWorld "
// 3.调用sayHello方法
helloWorld.sayHello();
}
}
As you can see from the code, the first step in using the Spring framework is to create a spring IOC container using the Spring application context:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
So here's a look at the spring applicationcontext of things.
Spring comes with several types of application contexts:
Spring context |
Describe |
Classpathxmlapplicationcontext |
The context definition is loaded from the XML configuration file under the Classpath, and the application context definition file is treated as a class resource. |
Filesystemxmlapplicationcontext |
Reads the XML configuration file under the file system and loads the context definition. |
Xmlwebapplicationcontext |
Read the XML configuration file under the Web app and load the context definition. |
We will slowly explain the spring-based Web application later, and we will be explaining the xmlwebapplicationcontext in detail. Now let's simply load the application context from the file system using Filesystemxmlapplicationcontext, or load the application contexts from the classpath using Classpathxmlapplicationcontext.
The process of loading the bean into the bean factory is similar, whether it is loading the application context from the file system or loading the application context from the classpath. For example, the following code shows how to load a file-systemxmlapplicationcontext:
ApplicationContext context = new FileSystemXmlApplicationContext("d:/applicationContext.xml");
Similarly, you can use Classpathxmlapplicationcontext to load the application context from the application's classpath:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Using Filesystemxmlapplicationcontext and using the Classpathxmlapplicationcontext
differenceIs
- Filesystemxmlapplicationcontext Locate the Applicationcontext.xml file under the specified file system path ;
- Classpathxmlapplicationcontext is found under all classpath (including jar files) Applicationcontext.xml . xml file.
represents the two ways I used to write Applicationcontext.xml storage locations:
Previously also expressed doubts about the location of the Applicationcontext.xml storage, is it possible to find it by just a name? Now it's clear thatClasspathxmlapplicationcontext is found under all classpath (including jar files) Applicationcontext.xml. xml file, so both of these methods can be found in the spring IOC container.
With an existing application context reference, you can invoke the Getbean () method of the application context to get the bean from the spring container.
// 2. 从IOC容器中获取Bean实例
helloWorld = (HelloWorld)context.getBean("helloworld");
[Spring Combat Series] (5) Spring Application context