一个PHP程序员的个人博客-司空如风

什么是应用程序中的上下文?

一、与上下文有关的常见关键字:

1、servletContext:Web容器的上下文环境
2、ApplicationContext: Spring 的上下文环境

其实编程语言中的“上下文”这个词的含义和做阅读理解中的上下文语境的含义是及其相似的,拿Java语言来举例,我们在编写一个测试类的时候经常要先加载程序的上下文,其实就是[scode type="blue"]加载程序需要的“运行环境”[/scode]

二、举个例子:

//加载ApplicationContext
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring配置文件
@ContextConfiguration(locations = "classpath:config/spring.xml")
public class MetaTool extends AbstractJUnit4SpringContextTests {
    @Test
    public void testContext(){
    //TestService 是一个bean 存储在 spring容器中
        TestService service = applicationContext.getBean(TestService.class);
        System.out.println(service);
    }
}


在这个例子中,我们如果想使用TestService这个bean,就必须要Spring这个环境,Spring就是TestService的上下文环境。
就像一段话,中间的句子必须依赖它前后的句子才能形成一段话: Spring(相当于句子前一句)
TestService(相当于句子中间一句)
Spring(相当于句子后一句)

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »