国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring 如何在 WEB 應用中使用

2019-11-14 23:02:43
字體:
來源:轉載
供稿:網友
SPRing 如何在 WEB 應用中使用

1. Spring 如何在 WEB 應用中使用 ?

1). 需要額外加入的 jar 包:

spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar

2). Spring 的配置文件, 沒有什么不同

3). 如何創建 IOC 容器 ?

①. 非 WEB 應用在 main 方法中直接創建 ②. 應該在 WEB 應用被服務器加載時就創建 IOC 容器:

在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中創建 IOC 容器.

③. 在 WEB 應用的其他組件中如何來訪問 IOC 容器呢 ?

在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中創建 IOC 容器后, 可以把其放在 ServletContext(即 application 域)的一個屬性中.

④. 實際上, Spring 配置文件的名字和位置應該也是可配置的! 將其配置到當前 WEB 應用的初始化參數中較為合適.

4). 在 WEB 環境下使用 Spring

①. 需要額外加入的 jar 包:

spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar

②. Spring 的配置文件, 和非 WEB 環境沒有什么不同

③. 需要在 web.xml 文件中加入如下配置:

<!-- 配置 Spring 配置文件的名稱和位置 --> <context-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath:applicationContext.xml</param-value> </context-param>

<!-- 啟動 IOC 容器的 ServletContextListener --> <listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

2. Spring 如何整合 Struts2 ?

1). 整合目標 ? 使 IOC 容器來管理 Struts2 的 Action!

2). 如何進行整合 ?

①. 正常加入 Struts2

②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action 注意: 在 IOC 容器中配置 Struts2 的 Action 時, 需要配置 scope 屬性, 其值必須為 prototype

<bean id="personAction"     class="com.atguigu.spring.struts2.actions.PersonAction"     scope="prototype">     <property name="personService" ref="personService"></property>    </bean>

③. 配置 Struts2 的配置文件: action 節點的 class 屬性需要指向 IOC 容器中該 bean 的 id

<action name="person-save" class="personAction">     <result>/success.jsp</result> </action>

④. 加入 struts2-spring-plugin-2.3.15.3.jar

3). 整合原理: 通過添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 會先從 IOC 容器中 獲取 Action 的實例.

if (appContext.containsBean(beanName)) {     o = appContext.getBean(beanName); } else {     Class beanClazz = getClassInstance(beanName);     o = buildBean(beanClazz, extraContext); }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南平市| 都江堰市| 泸州市| 洛隆县| 丹凤县| 涡阳县| 合川市| 枞阳县| 新竹市| 莱阳市| 那曲县| 佛坪县| 米易县| 清水河县| 锡林郭勒盟| 喜德县| 泽州县| 沈丘县| 耒阳市| 葫芦岛市| 澄江县| 富蕴县| 安乡县| 右玉县| 扎鲁特旗| 清丰县| 三原县| 都安| 闵行区| 巫溪县| 资源县| 孝感市| 杭锦后旗| 宕昌县| 鄂温| 成都市| 禄丰县| 望江县| 金华市| 遂平县| 海伦市|