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类中的方法,“(..)”为方法中的参数

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