zoukankan      html  css  js  c++  java
  • ssh框架构建项目详解--spring的xml配置详解

       在ssh框架中,spring处于管理者的位置,要对hibernate和struts进行管理

        其中hibernate是用来做持久层的(dao),hibernate对jdbc做了很好的封装,程序员在写dao层就不再需要书写繁琐的sql,

        Struts是做应用层的,他负责调用业务逻辑service层,代替sevlet,由struts来作为控制层  

        ssh的大致流程就是jsp-->struts-->service-->hibernate

       struts负责控制service层,从而控制了service的生命周期,这样层与层之间的久很强,属于耦合.这时使用spring框架就起到了控制Action和service的作用,两者之间就很松散,Spring的Ioc机制(也就是控制反转和依赖注入)正式用在此处

        简单理解,就是在action中,不再需要去new对象,而是用过spring进行自动注入,包括po实体类和service对象都通过springIoc进行自动注入

        1.控制反转:  依赖关系传统做法是通过代码直接操控,通过spring控制反转交给容器处理

        2.依赖注入:组件之间的依赖关系由容器决定,由容器将依赖关系注入到组件之中

       使用spring框架的第二个好处:

        1.在jdbc中事务提交的异常处理都是通过try/catch来完成的,我们需要手动进行提交,回滚等步骤,现在我们吧事务管理交给hibernate处理,是通过

          SessionFactory的创建和维护来处理事务的,而Spring也对SessionFactory进行了整合,因此我们不需要再hibernate-config.xml中对     SessionFactory进行配置

      

       spring框架需要的jar包自行百度..这里只写一下spring的基本配置

        

    <?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

    ">

    <!-- 使用annotation(注解) -->
    <context:annotation-config></context:annotation-config>
    <!-- 自动扫描Action,Service,Dao,po -->
    <!-- <context:component-scan base-package="com.lemon.action,com.lemon.service,com.lemon.dao,com.lemon.po"></context:component-scan> -->
    <context:component-scan base-package="com.crm"></context:component-scan>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="jdbc:mysql://localhost:3306/crm?characterEncoding=UTF-8">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="ou134568"></property>
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>

    <!-- 生成SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- 使用某个数据源生成SessionFactory -->
    <property name="dataSource" ref="dataSource"></property>
    <!-- 配置Hibernate的相关属性 -->
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="annotatedClasses">
    <list>
    <!-- <value>com.lemon.po.User</value>
    <value>com.lemon.po.Department</value> -->
    <value>com.crm.po.CrmCustomer</value>
    <value>com.crm.po.CrmCustomerContact</value>
    <value>com.crm.po.SysCustomerLevel</value>
    <value>com.crm.po.SysCustomerState</value>
    <value>com.crm.po.SysUserInfo</value>
    </list>
    </property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 配置事务管理的通知 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 配置事务的传播特性 -->
    <tx:method name="find*" read-only="true"/>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="query*" read-only="true"/>
    <!-- 本操作内如果有事务,则使用事务,如果没有,则开启新的事务 -->
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>

    <!-- 切面配置 -->
    <aop:config>
    <!-- <aop:pointcut expression="execution(* com.lemon.service.impl.*.*(..))" id="transactionPointcut"/> -->
    <aop:pointcut expression="execution(* com.crm..service.impl.*.*(..))" id="transactionPointcut"/>
    <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>

    </beans>

        

         

  • 相关阅读:
    距离计算方法总结 转自http://www.cnblogs.com/xbinworld/archive/2012/09/24/2700572.html#2663469
    2014.11.20查看到的有用网站和资料
    转载的计算机视觉方面的牛人博客,出处:blog.csdn.net/carson2005
    Java spring boot 2.0连接mysql异常:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
    Spring 复习第一天
    mysql 5.6.43免安装版安装教程
    JVM内存限制(最大值)
    修改电脑的ip
    Oracle 维护
    Oracle 操作
  • 原文地址:https://www.cnblogs.com/ou134568/p/6916934.html
Copyright © 2011-2022 走看看