Loading... ##一、与上下文有关的常见关键字: 1、servletContext:Web容器的上下文环境 2、ApplicationContext: Spring 的上下文环境 其实编程语言中的“上下文”这个词的含义和做阅读理解中的上下文语境的含义是及其相似的,拿Java语言来举例,我们在编写一个测试类的时候经常要先加载程序的上下文,其实就是<div class="tip inlineBlock info"> 加载程序需要的“运行环境” </div> ##二、举个例子: //加载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(相当于句子后一句) 最后修改:2019 年 09 月 16 日 08 : 50 AM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 ×Close 赞赏作者 扫一扫支付 支付宝支付 微信支付