[Spring practice series] (5) Spring application context

Source: Internet
Author: User

[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");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.