1 <?xml version="1.0" encoding="UTF-8"?>
2 <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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
3 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
4 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
5 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
6
7 <!-- 配置自动扫描的包 -->
8 <context:component-scan base-package="com.mcs.hibernate"></context:component-scan>
9
10 <!-- 配置数据源 -->
11 <!-- 导入资源文件 -->
12 <context:property-placeholder location="classpath:db.properties"/>
13 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
14 <property name="user" value="${jdbc.user}"></property>
15 <property name="password" value="${jdbc.password}"></property>
16 <property name="driverClass" value="${jdbc.driverClass}"></property>
17 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
18
19 <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
20 <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
21 </bean>
22
23 <!-- 配置Hibernate SessionFactory -->
24 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
25 <property name="dataSource" ref="dataSource"></property>
26
27 <!-- 加载Hibernate的配置文件 -->
28 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
29 <!-- 加载Hibernate 映射文件的位置及名称, 可以使用通配符 -->
30 <property name="mappingLocations" value="classpath:com/mcs/hibernate/entities/*.hbm.xml"></property>
31 </bean>
32
33 <!-- 配置 Spring 的声明式事务 -->
34 <!-- 1. 配置事务管理器 -->
35 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
36 <property name="sessionFactory" ref="sessionFactory"></property>
37 </bean>
38
39 <!-- 2. 配置事务属性, 需要事务管理器 -->
40 <tx:advice id="txAdvice" transaction-manager="transactionManager">
41 <tx:attributes>
42 <tx:method name="get*" read-only="true"/>
43 <tx:method name="*"/>
44 </tx:attributes>
45 </tx:advice>
46
47 <!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
48 <aop:config>
49 <aop:pointcut expression="execution(* com.mcs.hibernate.service.*.*(..))"
50 id="txPointcut"/>
51 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
52 </aop:config>
53
54 </beans>