Spring4的知识应用总结(七)—— Spring整合Struts2框架

Spring整合Struts2



一、Spring在Web中的使用

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

具体实现

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


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

        <!-- 启动 IOC 容器的 ServletContextListener (服务器加载是就创建IOC容器)-->
        <listener>
                <listener-class>
org.springframework.web.context.ContextLoaderListener </listener-class>
        </listener>


注意:还需要额外的jar包 Spring-web-4.0.0.RELEASE.jar 与Spring-webmvc-4.0.0.RELEASE.jar
        ioc容器在非web中可以直接创建(之前已经写了)但在web中应该在web应用被服务器加载时创建


二、整合Struts2

        Struts2 通过插件实现和 Spring 的整合. 
        Struts2 提供了两种和 Spring整合基本的策略:
                第一种:将 Action 实例交给 Spring 容器来负责生成, 管理, 通过这种方式, 可以充分利用 Spring 容器的 IOC 特性, 提供最好的解耦
                第二种:利用  Spring 插件的自动装配功能, 当 Spring 插件创建 Action 实例后, 立即将 Spring 容器中对应的业务逻辑组件注入 Action 实例. 

第一种:让 Spring 管理控制器

        将 Action 实例交给 Spring 容器来负责生成, 管理, 通过这种方式, 可以充分利用 Spring 容器的 IOC 特性, 提供最好的解耦
        整合流程:
                安装 Spring 插件: 把struts2-spring-plugin-2.2.1.jar 复制到当前 WEB 应用的 WEB-INF/lib 目录下
                在 Spring 的配置文件中配置 Struts2 的 Action 实例
                在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID


第二种:自动装配(比较麻烦 就不写代码了)

        利用  Spring 插件的自动装配功能, 当 Spring 插件创建 Action 实例后, 立即将 Spring 容器中对应的业务逻辑组件注入 Action 实例. 
        配置自动装配策略: Spring 插件的自动装配可以通过struts.objectFactory.spring.autoWire常量指定, 该常量可以接受如下值:
                name: 根据属性名自动装配. 
                type: 根据类型自动装配. 若有多个 type 相同的 Bean, 就抛出一个致命异常; 若没有匹配的 Bean, 则什么都不会发生, 属性不会被设置
                auto: Spring 插件会自动检测需要使用哪种方式自动装配方式
                constructor: 同 type 类似, 区别是 constructor 使用构造器来构造注入所需的参数
        整合流程:
                安装 Spring 插件
                正常编写 struts 配置文件
                编写 spring 配置文件, 在该配置文件中不需要配置 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.2.1.jar这个包的作用其实就是将Struts的对象工厂(strutsObjectFactory)拦截器换成了Spring的对象工厂




下一章节:Java轻松玩转扫码登录
上一章节:Spring4的知识应用总结(六)——Spring事务管理机制


猜你喜欢

转载自blog.csdn.net/qq_25814003/article/details/53899955