2. 當設計Web應用時, 在 Action 中使用以下代碼,獲取單例的 ApplicationContext 對象:
private ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
這是獲取 ServletContext 域中的 ApplicationContext (Map形式), 能夠獲取的前提是已經在"web.xml"中註冊了 Spring 的 <listener> 和 <context-param>, 用於在web聲明週期開始時到指定目錄加載"applicationContext.xml"文件, 具體配置爲: <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:gr/applicationContext.xml</param-value>
</context-param>
3. 如果不需要自己調用 ApplicationContext, 並且已經將需要生成的Bean配置進"applicationContext.xml"時, 可以使用整合.除了添加(2)的配置, 還要引入jar包:"struts2-spring-plugin-2.1.6.jar"(引入即可, 不必用它做任何額外的事情). 這樣一來除了會在項目開始時就加載單例的 ApplicationContext, Spriing 還會代替 Struts 行使某些功能, 如 Action 的創建.