Spring 承擔 IOC 容器角色,管理 Struts2 Action 與 Hibernate DAO/Service 的生命周期與依賴關係,實現組件解耦。
new,單例/原型模式控制創建策略,節省內存。lib/ 目錄下所有 jar(排除 javadoc/sources)。commons-logging.jar、struts2-spring-plugin.jar (Struts2 整合橋接)。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<constant name="struts.objectFactory" value="spring" />

<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="...spring-beans-4.2.xsd ...spring-context-4.2.xsd ...spring-tx-4.2.xsd">
<bean id="myServiceImp" class="services.MyServiceImp" scope="singleton"/>
<bean id="myConnectionImp" class="util.MyConnectionImp" scope="prototype"/>
<bean id="actionService" class="action.ActionService" scope="prototype">
<property name="ms" ref="myServiceImp"/> <!-- 注入 Service -->
</bean>

| Scope | 含義 |
|---|---|
| singleton | 容器內單實例(默認),適用無狀態 Bean |
| prototype | 每次請求創建新實例 |
| request | 每個 HTTP 請求一實例 |
| session | 每個 HTTP Session 一實例 |
| global session | Portlet 環境下全局 Session 一實例 |
<property name="ms" ref="myServiceImp"/>:Setter 注入,ActionService 需有 setMs(MyServiceImp ms)。
scope="prototype":每次獲取 Bean 重新創建(如數據庫連接)。Spring 通過 applicationContext.xml 集中管理組件定義與依賴關係,配合 Struts2 插件實現 Action 的 IOC 化,是 SSH 架構的粘合劑。