一. 定義頭:(為了后面定義標(biāo)簽)
<?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: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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init="true">
二.定義數(shù)據(jù)源
<!-- 定義數(shù)據(jù)源的Bean ,給Hibernate的sessionFactory-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/test">
</property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
三.定義SessionFactory
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="lobHandler">
<ref bean="lobHandler"/>
</property>
<property name="dataSource">
<ref bean="dataSource"
/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!--設(shè)置二級(jí)緩沖-->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<!--設(shè)置二級(jí)緩沖,打開查詢緩沖-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!--設(shè)置顯示Hibernate操作的SQL語句-->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/njwj/model/test.hbm.xml
</value>
</list>
</property>
</bean>
四.處理一些需要處理注入的Bean
<!-- 申明處理Clob對(duì)象LobHandler
-->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean id="TestHandlerDao" class="com.njwj.dao.TestHandlerDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="HandlerManager" class="com.njwj.service.HandlerManager">
<property name="saveTestHanlerdao" ref="TestHandlerDao"></property>
</bean>
五.配置事務(wù)管理器
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
六.配置事務(wù)管理特性
<!-- 配置事務(wù)特性,配置add、delete和update開始的方法,事務(wù)傳播特性為required-->
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
</tx:attributes>
</tx:advice>
七.配置哪些類進(jìn)行事務(wù)管理
<!-- 配置那些類的方法進(jìn)行事務(wù)管理,當(dāng)前com.njwj.service包中的子包、類中所有方法需要,還需要參考tx:advice的設(shè)置 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* com.njwj.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
八.配置Spring采用annotation(可選)
<context:annotation-config
/>
Web配置:
<!--定義Spring的配置的位置,可以定義多個(gè)配置文件,可以使用通配符。 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!--設(shè)置一起動(dòng)當(dāng)前的Web應(yīng)用,就加載Spring,讓Spring管理Bean-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--解決Hibernate延遲加載出現(xiàn)的問題,需要放到struts2過濾器之前-->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!--Struts2的過濾器,使用Struts2,必須配置該項(xiàng)-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!--解決Hibernate延遲加載出現(xiàn)的問題,仍需要放到struts2過濾器mapping之前-->
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--Struts2的過濾器,配套的filter-mapping-->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
posted on 2009-05-10 21:33
孤飛燕 閱讀(1934)
評(píng)論(0) 編輯 收藏 所屬分類:
Spring