Spring4.0学习笔记--web下配置spring并整合Struts2

在通用的web 应用中访问Spring

•通过注册 Servlet 监听器 ContextLoaderListener, Web 应用程序可以加载 Spring 的ApplicationContext 对象. 这个监听器会将加载好的ApplicationContext 对象保存到 Web 应用程序的 ServletContext 中. 随后, Servlet 或可以访问 ServletContext 的任意对象就能通过一个辅助方法来访问 Spring 的应用程序上下文了.

•在 web.xml 文件中注册 Spring 提供的 Servlet 监听器, 它会在当前 web 应用被加载时将 Spring 的 ApplicationContext 保存到 ServletContext 对象中.
监听器通过查找 web 应用初始化参数 contextConfigLocation 来获取 Bean 配置文件的位置. 如果有多个 Bean 配置文件, 可以通过逗号或空格进行分隔. contextConfigLocation 的默认值为 /WEB-INF/applicationContext.xml.若实际的文件和默认值一致则可以省略这个 web 应用的初始化参数


web 应用程序中访问Spring ApplicationContext对象
•可以通过的静态方法
来获取 Spring 的 ApplicationContext 对象


Spring整合 Struts
•通过注册 Servlet 监听器, Struts 应用程序能够加载 Spring 的 ApplicationContext 对象,并像在通用的 Web 应用程序中那样在 Servlet 上下文中对它进行访问. 然而, Spring 还提供了更好的, 特定于 Struts 的解决方案.
–在 struts 配置文件中注册 Struts 插件来加载应用程序上下文, 它会自动引用 Servlet 监听器加载的应用程序上下文作为它的父上下文, 以便可以引用其中声明的 Bean
–Spring 提供了一个 ActionSupport 对象, 这是 Action 类的一个子类, 通过它的 getWebApplicationContext() 方法可以获取到 Spring 的应用程序上下文
Spring的应用程序上下文中声明StrutsAction 对象,使用Spring 的依赖注入来注入Spring 应用程序上下文的其他Bean

Spring 的应用程序上下文加载到Struts 应用程序中(1)

•将 Spring 的应用程序上下文加载到 Struts 应用程序中
–在 web.xml 文件中注册 Servlet 监听器这个监听器会默认加载 /WEB-INF/applicationContext.xml作为 Spring 的配置文件.  因而无需显式地指定它的位置


Spring Bean 配置文件中声明Struts Action

•除了在 struts 动作中通过 Spring 应用程序中主动查找 Spring Bean 之外, 还可以使用依赖注入模式将Spring 中声明的 Bean 注入到 Struts 动作中.
applicationContext.xml中声明 Struts Action要求该 Bean name 必须和它在 struts-config.xml 文件中的路径一致. 因为该 <bean> 元素的 id 属性不能包含 / 字符, 所以应该用 name 属性代替.
•还必须注册 struts 请求处理器Struts 匹配动作路径和 Bean 名称, 从而在 Spring 的应用程序上下文中查找相应的动作实例. 注册了这个请求处理器之后, 在 struts-config.xml中就不需要指定 type 属性了
•若已经在 struts-config.xml 文件中注册了一个请求处理器, 可以将所有 action 节点的 type 属性指定为

web.xml


struts-config.xml

applicationContext.xml


Spring 的应用程序上下文加载到Struts 应用程序中(2)
•在 struts 配置文件 struts-config.xml 文件中注册默认情况下, 该插件会利用 web.xml 文件中注册的 ActionServlet 实例的名称加上 –servlet.xml后缀作为文件名. 如果想要另外加载一个Bean 配置文件, 可以在 contextConfigLocation 属性中指定文件名. 但此时需通过 servlet 配置 Spring 容器随 Web 应用的启动而初始化. 而不适用 Listener 配置.
•如果 applicationContext.xml文件和 action-servlet.xml 文件同时存在, struts 插件加载的 Spring 应用程序上下文会自动引用在 applicationContext.xml 中的配置信息作为父上下文. 业务服务通常配置在 applicationContext.xml 中, 而 web 相关组件配置在 action-servlet.xml 中.

web.xml
struts-config.xml
applicationContext.xml
action-servlet.xml



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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,可以得出结论:spring-boot-starter-web依赖包含了开发web项目所需的相关配置。具体来说,它会自动引入web模块开发所需的相关jar包,包括但不限于处理HTTP请求的类、处理URL映射的类、处理模板引擎的类等。此外,根据引用\[3\]的内容,spring-boot-starter-web还包含了一些自动配置和默认配置,例如默认的日志配置和YAML配置。总之,通过引入spring-boot-starter-web依赖,我们可以方便地进行web项目的开发,并且无需手动配置大部分相关内容。 #### 引用[.reference_title] - *1* [附加:Spring Boot项目中,spring相关依赖分析;(主要是spring-boot-starter依赖、spring-boot-starter-web依赖等)](https://blog.csdn.net/csucsgoat/article/details/125354054)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springboot之核心依赖spring-boot-starter,spring-boot-starter-parent,spring-boot-starter-web依赖解析](https://blog.csdn.net/qq_37164847/article/details/89508910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值