Spring的applicationContext.xml配置

applicationContext.xml:
1、首先中加入

xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd"

是爲了<tx:advice> 不報錯。
2.加入

<context:annotation-config />

是爲了AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 這 4 個BeanPostProcessor。
註冊這4個 BeanPostProcessor的作用,就是爲了你的系統能夠識別相應的註解。
3、加入

<context:component-scan
base-package="com.lua.ssh.daoImpl,com.lua.ssh.serviceImpl,com.lua.ssh.action"></context:component-scan>

是爲了自動掃描包裏面包括的全部註解信息,並注入到Spring容器中。
4、加入

<!-- 定義數據源的信息 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@LOCALHOST:1521:ORCL</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>root123ROOT</value>
</property>
</bean>

配置數據庫的登錄信息。
5、加入

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />//配置數據庫的登錄信息
<property name="mappingLocations" value="classpath:com/lua/ssh/domain/*.hbm.xml"></property>//加載位於class下的.hbm.xml後綴的全部文件
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>//數據庫的方言
</props>
</property>
</bean>

6、事務管理

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

需要引用SessionFactory
7、
1、採用配置文件配置

<!-- 2.配置事務屬性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>

<!-- name:與事務屬性關聯的方法名。通配符(*)可以用來指定一批關聯到相同的事務屬性的方法。 -->
<!-- isolation:事務隔離級別 -->
<!-- propagation:事務傳播行爲 -->
<!-- read-only:事務是否只讀(典型地,對於只執行查詢的事務你會將該屬性設爲true, 如果出現了更新、插入或是刪除語句時只讀事務就會失敗) -->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="isLastNameValid" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>

2、可以利用註解方式配置

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

在java代碼中

@Transactional(readOnly = false)

即可
配置事務的屬性,出現哪些函數時回啓動事務,並設置相關屬性
8、

<!-- 3.配置事務切入點,再把事務屬性和事務切入點關聯起來 -->
<aop:config>
<aop:pointcut expression="execution(* com.lua.ssh.dao.*.*(..) )"
id="txPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>

事務切入點爲在哪個包下的哪個類中的哪個方法中哪些參數。
第一個爲返回值,第二個爲dao下的java類,第三個爲java類中的方法,“(..)”爲方法中的參數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章