日期:2014-05-20  浏览次数:20903 次

关于spring的问题,请个指教一下?
1 简述spring的特征及其主要作用?
2 jsp如何获得spring的上下文?

------解决方案--------------------
第 3 章 Spring

胶水技术,依靠DI黏合不同工具,让他们在一起运行。

我们选择Spring不光是为了IOC,而是为了将JavaEE中众多开源项目封装在一起进行应用。
3.1. 在项目中使用Spring
3.1.1. 直接构造ApplicationContext

ApplicationContext ctx = new ClasspathXmlApplicationContext("applicationContext.xml");


3.1.2. 从ServletContext取得web.xml中初始化的ApplicationContext

首先在web.xml中配置listener。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


然后从ServletContext中获得ApplicationContext。

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);


对于无法获得ServletContext的环境,最好自定义一个listener,将生成的ApplicationContext放入一个单例中,以便日后使用。