现在的位置: 首页 > 综合 > 正文

Spring总结及不同版本间的区别以及 Struts1.2+Spring1.2+Hibernate3.1的整合

2013年10月10日 ⁄ 综合 ⁄ 共 3564字 ⁄ 字号 评论关闭

一 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生成的影射文件中要取消指定的数据库名,否则查询数据库是要报错!

 

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

四 Struts1.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>

抱歉!评论已关闭.