SSH——Struts2中引入Spring

来源:互联网 发布:nba2k17詹姆斯身体数据 编辑:程序博客网 时间:2024/06/08 19:23


一,为什么要使用Spring


          1,装配JavaBean


                  摒弃老旧的new方式,spring为我们提供了一种机制,使得创建javaBean以及设置javaBean属性的工作可以通过配置文件以及Spring框架本身来完成。这样,当某些地方需要改变时,修改Spring的配置文件即可。这个过程实际上就是Spring框架通过读取相应的配置文件中的内容,并根据这些配置自动装在javaBean对象,设置JavaBean的属性。


           2,整合第三方框架。


                     spring的设计理念就是尽可能整合第三方框架,使得这些被整合的技术更容易使用,更易于维护,从而大大降低程序开发的难度。



 二,struts2 中引入Spring


           0,引入spring的jar包

                                                

                    com.springsource.org.apache.log4j-1.2.15.jar

                    spring-beans-3.2.0.RELEASE.jar

                    spring-context-3.2.0.RELEASE.jar

                   spring-core-3.2.0.RELEASE.jar

                   spring-expression-3.2.0.RELEASE.jar

                   spring-web-3.2.0.RELEASE.jar

                   struts2-spring-plugin-2.3.15.3.jar


           1,配置spring监听器

                                在我们的web.xml中,加入如下配置:

                

    <!-- spring 监听器的配置 -->   <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>

             

            ContextLoaderListener 这个类负责启动Web容器时,自动装配ApplicationContext的配置信息。


          2,applicationContext.xml 配置


                    我们在WEB-INF目录下面,建立applicationContext.xml配置文件(当然,也可建立在其他地方,叫其他的名字,这里我们采用最简单的方式配置)。


            例如:

         

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 装配AdditionCalc类 --><bean id="calculator" class="net.blogjava.nokiaguy.models.AdditionCalc"><property name="message"><value>{0}+{1}={2}</value></property></bean></beans>


         我们的action类如下:


       

package net.blogjava.nokiaguy.models;public class SpringCalcAction {private int operand1;private int operand2;//calculator属性由struts2插件自动装配,在spring配置文件中需要有同名的<bean>元素private Calculator calculator;public int getOperand1() {return operand1;}public void setOperand1(int operand1) {this.operand1 = operand1;}public int getOperand2() {return operand2;}public void setOperand2(int operand2) {this.operand2 = operand2;}public Calculator getCalculator() {return calculator;}public void setCalculator(Calculator calculator) {this.calculator = calculator;}public String execute(){//只需要调用Calculator对象的calc方法,在切换业务模型时候,不需要修改如下代码int value=calculator.calc(operand1, operand2);return "success";}}


           在上面的Action类中,存在类型为Calculator 的属性calculator。这个类型是一个父类。在spring的配置文件中,我们为这个属性配置了装配类,当调用Action的时候,自动将子类实例装配到父类对象中。注意,spring中,bean的id要跟action中的属性名称保持一致。






2 0
原创粉丝点击