Spring总结及不同版本间的区别

来源:互联网 发布:适合20岁女装淘宝店铺 编辑:程序博客网 时间:2024/04/28 18:56

Spring总结及不同版本间的区别
来源:西安通瀛  作者:  时间:2010-11-26 09:52:00  浏览:4090次    

一 Spring中有一个IOC对象容器(Spring内部的一个HashMap容器),用于盛放对象的!

Spring中全部生成的对象都放在IOC对象容器中的!是以name或id的值做键存在容器中的!

 

给对象注入值的方式:---以下的4种全部都是交给Spring框架去注入的!

1.       用不带参数的构造子依赖注入

2.       Set依赖注入

3.       带参数的构造子依赖注入

4.       接口依赖注入

5.       LookUp-Method(使用CGLIB的动态字节码增强功能)注入,重写方法!

<ref bean="p3"/>---表示可以引用其他Xml中的Bean;

<ref local="a"/>---local表示只能引用本Xml中的Bean;

Spring中加载上下文的三种方式:

ApplicationContext act=new ClassPathXmlApplicationContext(“applicationContext.xml”);

Resource resource=new ClassPathResource("application.xml");

ApplicationContext  act =  new  FileSystemXmlApplicationContext (“F:/绝对路径/application.xml”) ;

二 Spring配置工厂Bean

调用getBean(“工厂bean的id”)方法,Spring返回的不是直接创建的工厂Bean的实例,而是由工厂Bean创建的Bean实例(工厂bean生产的产品实例).

三 Spring的IOC容器用于管理Struts和Hibernate

 SessionFactory和事务都是Hibernate中的东西,而不是Spring 的,Spring只是进行了封装,本质上是Hibernate的,Spring通过SpringORM封装了Hibernate!

 

注意:在使用Spring的时候,由Hibernate生成的影射文件中要取消指定的数据库名,否则查询数据库是要报错!

 

----------整合部分!

四 Stirng1.2+Spring1.2+Hibernate3.1的整合(重点)

 

 

1.        Struts1.2的配置文件: Struts-config.xml文件都是自动生成的!

  <action-mappings >

    <action

      attribute="userForm"  

      parameter="m"

      path="/user"

      name="userForm"

      type="org.springframework.web.struts.DelegatingActionProxy"—不同点就在这里,不是Action的包路径了,而是使用Spring的代理类交给Spring去创建Action的事例!让它享受Spring的服务

      validate="false">

      <forward name="user" path="/user.jsp"></forward>

     </action>

</action-mappings>

  <message-resources parameter="org.tongying.www.ApplicationResources" />

</struts-config>

 

 

 

2.  Spring的配置文件 ApplicationContext.xml文件头文件自动生成

<beans>

<!--session工厂-->

使用的事务和事务的管理都是用的Hibernate里面的东西,都在Spring的.orm.hibernate3的下面

<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

     <property name="configLocation">

     <value>classpath:hibernate.cfg.xml</value>

     </property>

</bean>

<!--事务管理-->

<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sf"></property>

</bean>

配置的继承

下面的配置可以使用配置继承的方法来减少Spring的配置泛滥

先写个基础的代理,不指定target的代理目标,并且告诉Spring是抽象的不能使用,abstract=”true”!

当建对于具体的目标的Bean的时候,继承上面的基础代理就可以了!parent=”baseProxy”

再把target指定代理目标!

<!--servicei代理-->这就是代理,可以在其中加工处理的!

用事务拦截机的代理工厂Bean,针对业务层进行事务的监控--

<bean id="basePoxy"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

    <property name="transactionManager" ref="transactionManager"></property>

       <property name="transactionAttributes">

        <props>

            <prop key="insert*">PROPAGATION_REQUIRED</prop>

            <prop key="delete*">PROPAGATION_REQUIRED</prop>

            <prop key="update*">PROPAGATION_REQUIRED</prop>

            <prop key="save*">PROPAGATION_REQUIRED</prop>

            <prop key="merge*">PROPAGATION_REQUIRED</prop>

            根据不同的动作而执行事务!   

            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

        </props>

    </property>

</bean>

 

针对业务层进行事务的监控

<bean id="userserviceproxy"  parent="baseproxy">

    <property name="target" ref="userservicei"></property>     

</bean>

<!--daoimpl-->要使用SessionFactory查询持久层!

<bean id="userdaoi" class="org.wllt.www.daoi.UserDaoi">

  <property name="sessionFactory" ref="sf"></property>

</bean>

 

<!--serviceimpl-->要使用Dao接口的数据

<bean id="userservicei" class="org.wllt.www.servicei.UserServicei">

  <property name="dao" ref="userdaoi"></property>

</bean>

 

<!--Action-->要使用业务层的数据

<bean name="/user" class="org.wllt.www.action.UserAction" singleton=”true”>--默认就是true

  <property name="userservice" ref="userservicePoxy"></property>

但是不能直接来自于Servicei,因为在业务层需要事务,数据库才会生效!只有先用代理处理!

</bean>

</beans>

3.Web.xml文件

Struts1.2的配置文件自己会加载的,但是Spring则要我们自己来写配置!

---上下文的加载Spring的配置文件!

<context-param>     

 <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext.xml</param-value>

 </context-param>

---Spring加载监听器

   <listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

注意:在整合的过程中,Spring的jar包和Hibernate的jar包有许多的冲突,我们的处理方法是把Hibernate的兼容性先添加到项目中,让它可以帮我们产生配置文件和po影射文件,再移出全部的jar 包,把Hibernate的一个.jta拷到项目中就够使用了!。在和Struts2.0的整合过程中也是如此!(只不过还有一个Struts2到Spring的插件.jar)

两个.jar包都是倒到项目的WebRoot下的Web-Inf下的lib下就可以了!

Struts1.2和Struts2.0配合Spring1.2时的区别:

(1)1.2在Struts-config.xml中使用代理类来来把Action的事例交给Spring的

org.springframework.web.struts.DelegatingActionProxy

     2.0中是在Actions.xml中使用class=”Action在Spring 中的id名”

 (2)1.2在Application中定义Action是使用的name=”/useraction”

2.0 -------------------------------  id=”useraction”  

 (3)2.0在struts.xml中使用了<constant name="struts.objectFactory" value="spring"/>

(4)web.xml都是一样的!

五.Struts2.0+Spring1.2+Hibernate3.1的整合!

1.自己建actions.xml文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "struts-2.0.dtd">  

 <struts>

    <package name="one" namespace="/one" extends="struts-default">

 

    <!--利用class="useraction"到Spring容器中去找生成的对象-->

    <action name="user" method="list" class="useraction">-原来是action的包路径,Spring整合不同

     <result name="user">/user.jsp</result>

        </action>     

    </package>

 </struts>

2.自己建struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "struts-2.0.dtd">

   

    <struts>

      <constant name="struts.devMode" value="true" />

      <constant name="struts.locale" value="zh_CN" /> 

      <constant name="struts.i18n.encoding" value="GBK" />

      <constant name="struts.action.extension" value="action,itfuture,do"/>

     

      <!--利用Struts2到Spring的插件把Struts和Spring联系起来 -->[不用也可以]

      <constant name="struts.objectFactory" value="spring"/>

      <!--加载了action.xml-->

      <include  file="actions.xml"/>

</struts>

3.Spring中的applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--SessionFactory-->

   <bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="configLocation">

       <value>classpath:hibernate.cfg.xml</value>

       </property>

   </bean>

<!--事务的管理-->

   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sf"></property>

   </bean> 

<!--serviceiproxy用事务拦截机的代理工厂Bean,针对业务层进行事务的监控,可以先做成代理的一个模版(其他的地方引用即可!)-->

<bean id="baseproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

     <property name="transactionManager" ref="transactionManager"></property>

     //去掉--<property name="target" ref="servicei"></property>--//

     <property name="transactionAttributes">

         <props>  

            <prop key="save*">PROPAGATION_REQUIRED</prop>

            <prop key="delete*">PROPAGATION_REQUIRED</prop>

            <prop key="update*">PROPAGATION_REQUIRED</prop>   

            <prop key="insert*">PROPAGATION_REQUIRED</prop>

            <prop key="merge*">PROPAGATION_REQUIRED</prop>

            

            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

         </props>

     </property>

</bean>

此处引用定义代理(都可以适用的,部分什么版本的配合!)

<bean id="deptserviceiproxy" parent="baseproxy">

    <property name="target" ref="deptservicei"></property>

</bean>

 

<!--daoimpl-->   

<bean id="daoi" class="org.wllt.www.daoi.UserDaoi">

  <property name="sessionFactory" ref="sf"></property>

</bean>

 

<!--serviceimpl-->

    <bean id="servicei" class="org.wllt.www.servicei.UserServicei">

        <property name="dao" ref="daoi"></property>

</bean>

 

<!--action-->

    <bean id="useraction" class="org.wllt.www.action.UserAction" singleton=”true”>

    <property name="service" ref="serviceproxy"></property>

    </bean>   

</beans>

 

LookUp(利用CGLIB增强技术完成)

<lookup-method name=”方法名” bean=”返回类型”>

六.Spring2.0的使用!

(Struts2.0+Spring2.0+Hibernate3.1)

1.       Spring2.0完全向下兼容Spring1.2,代码不经过改动就可以运行项目!

2.       Spring1.2和Spring2.0的区别在哪里?

区别就是在applicationContext.xml的文件中!(在.jar包里有)

a.头文件不同1.2是.dtd规则,而2.0是.xsd规则

b.下面的<action>配置单态性不同!

<!--action-->

  <!--spring2.0用scope="singleton/prototype"替代spring1.2中的singleton="true/false"-->

  <bean id="deptAction" class="org.itfuture.www.actions.DeptAction" scope="prototype">

     <property name="service" ref="deptserviceiproxy"></property>

  </bean>

 

0 0