beans.xml,springbeans.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
10 http://www.springframework.org/schema/aop
11 http://www.springframework.org/schema/aop/spring-aop.xsd" >
12 <!-- 打開注解注入 -->
13 <context:annotation-config/>
14 <context:component-scan base-package="com.hkwy" />
15 <!-- 添加AOP annotation支持 -->
16 <aop:aspectj-autoproxy/>
17 <!-- 配置數據源 -->
18 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
19 <!-- 基本參數 -->
20 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
21 <property name="url" value="jdbc:mysql://localhost:3306/spring1"></property>
22 <property name="username" value="root"></property>
23 <property name="password" value="root"></property>
24 <!--配置連接池的參數 -->
25 <property name="initialSize" value="1"></property>
26 <!-- 連接池的最大數量 -->
27 <property name="maxActive" value="500"></property>
28 <!-- 空閒時 連接數降低到最小值 -->
29 <property name="maxIdle" value="2"></property>
30 <!-- 空閒時連接池的最小值 -->
31 <property name="minIdle" value="1"></property>
32 <!-- 最大等待時間 -->
33 <property name="maxWait" value="1000"></property>
34 </bean>
35 <!--配置hibernate SessionFactory annotation方式 -->
36 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
37 <!-- 將數據源配置進來 -->
38 <property name="dataSource" ref="dataSource"></property>
39 <!--對應的實體類,只需要指定包 -->
40 <property name="packagesToScan">
41 <value>com.hkwy.entity</value>
42 </property>
43 <!-- 配置hibernate其他參數 -->
44 <property name="hibernateProperties">
45 <props>
46 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
47 <prop key="hibernate.show_sql">true</prop>
48 <prop key="hibernate.format_sql">true</prop>
49 <prop key="hibernate.hbm2ddl.auto">update</prop>
50 </props>
51 </property>
52 </bean>
53 <!--將hibernate交給Spring管理其事務 -->
54 <!--配置事務管理器 -->
55 <bean id="txmanager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
56 <property name="sessionFactory" ref="sessionFactory"></property>
57 </bean>
58 <!--配置Spring的AOP 來管理事務 -->
59 <aop:config>
60 <!-- 配置需要管理的dao -->
61 <aop:pointcut expression="execution (* com.hkwy.dao.*.*(..))" id="aoph"/>
62 <!--通過advisor來確定具體要加入事務控制的方法 -->
63 <aop:advisor advice-ref="txadvice" pointcut-ref="aoph"/>
64 </aop:config>
65 <!-- 配置advice 來確定哪些數據操作需要加入到事務控制 -->
66 <tx:advice id="txadvice" transaction-manager="txmanager">
67 <tx:attributes>
68 <tx:method name="*" propagation="REQUIRED"/>
69 </tx:attributes>
70 </tx:advice>
71
72 </beans>