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>

 

    

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM