Spring的IoC容器-Spring ApplicationContext容器

Application Context是spring中较高级的容器。和BeanFactory类似,它可以加载配置文件中定义的bean,将所有的bean集中在一起,当有请求的时候分配bean。 另外,它增加了企业所需要的功能,比如,从属性文件从解析文本信息和将事件传递给所指定的监听器。这个容器在org.springframework.context.ApplicationContext interface接口中定义。

ApplicationContext包含BeanFactory所有的功能,一般情况下,相对于BeanFactory,ApplicationContext会被推荐使用。BeanFactory仍然可以在轻量级应用中使用,比如移动设备或者基于applet的应用程序。

最常被使用的ApplicationContext接口实现:

  • FileSystemXmlApplicationContext:该容器从XML文件中加载已被定义的bean。在这里,你需要提供给构造器XML文件的完整路径

  • ClassPathXmlApplicationContext:(最常用)该容器从XML文件中加载已被定义的bean。在这里,你不需要提供XML文件的完整路径,只需正确配置CLASSPATH环境变量即可,因为,容器会从CLASSPATH中搜索bean配置文件。

  • WebXmlApplicationContext:该容器会在一个web应用程序的范围内加载在XML文件中已被定义的bean。

例子实践:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jsoft.testspring</groupId>
  <artifactId>testapplicationcontext</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testapplicationcontext</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring Core -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
     
    <!-- Spring Context -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    
  </dependencies>
</project>

HelloWorld.java:

package com.jsoft.testspring.testapplicationcontext;

public class HelloWorld {
    private String messageString;
    
    public void setMessage(String message){
        this.messageString = message;
    }
    
    public String getMessage(){
        return this.messageString;
    }
}

beans.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <bean id="helloWorld" class="com.jsoft.testspring.testapplicationcontext.HelloWorld">
        <property name="message" value="Hello World!"/>
    </bean>
  
</beans>

App.java:

package com.jsoft.testspring.testapplicationcontext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\开发工程\GitHub\5_java_example\springtest\test3\testapplicationcontext\src\main\resources\beans.xml");
        HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
        System.out.println(helloWorld.getMessage());
    }
}

运行结果:

运行原理:

  • 第一步生成工厂对象。加载完指定路径下bean配置文件后,利用框架提供的FileSystemXmlApplicationContext API去生成工厂bean。FileSystemXmlApplicationContext负责生成和初始化所有的对象,比如,所有在XML bean配置文件中的bean。

  • 第二步利用第一步生成的上下文中的getBean()方法得到所需要的bean。 这个方法通过配置文件中的bean ID来返回一个真正的对象。一旦得到这个对象,就可以利用这个对象来调用任何方法。

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test3/testapplicationcontext

以上参考:http://wiki.jikexueyuan.com/project/spring/ioc-container/spring-application-context-container.html

原文地址:https://www.cnblogs.com/EasonJim/p/6875930.html