[Spring Combat Series] (5) Spring Application context

Source: Internet
Author: User


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

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.