SSH——Struts2中引入Spring


一,为什么要使用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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://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中的属性名称保持一致。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值