<?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: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/context http://www.springframework.org/schema/context/spring-context-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.xsd" >
<!-- 打开注解注入 -->
<context:annotation-config/>
<context:component-scan base-package="com.hkwy" />
<!-- 添加AOP annotation支持 -->
<aop:aspectj-autoproxy/>
<!-- 配置数据源 -->
<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/spring1"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<!--配置连接池的参数 -->
<property name="initialSize" value="1"></property>
<!-- 连接池的最大数量 -->
<property name="maxActive" value="500"></property>
<!-- 空闲时 连接数降低到最小值 -->
<property name="maxIdle" value="2"></property>
<!-- 空闲时连接池的最小值 -->
<property name="minIdle" value="1"></property>
<!-- 最大等待时间 -->
<property name="maxWait" value="1000"></property>
</bean>
<!--配置hibernate SessionFactory annotation方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 将数据源配置进来 -->
<property name="dataSource" ref="dataSource"></property>
<!--对应的实体类,只需要指定包 -->
<property name="packagesToScan">
<value>com.hkwy.entity</value>
</property>
<!-- 配置hibernate其他参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--将hibernate交给Spring管理其事务 -->
<!--配置事务管理器 -->
<bean id="txmanager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置Spring的AOP 来管理事务 -->
<aop:config>
<!-- 配置需要管理的dao -->
<aop:pointcut expression="execution (* com.hkwy.dao.*.*(..))" id="aoph"/>
<!--通过advisor来确定具体要加入事务控制的方法 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="aoph"/>
</aop:config>
<!-- 配置advice 来确定哪些数据操作需要加入到事务控制 -->
<tx:advice id="txadvice" transaction-manager="txmanager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> </beans>