class="spring的配置文件(beans)"><?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" 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-2.5.xsd 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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="cn.mrxia"></context:component-scan> <!--1 设置连接参数信息 --> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf8"></property> </bean> <!--2 把dataSource交给sessionFactory 在hibernate里面,想用注解需要创建AnnotationConfiguration,而在spring中用AnnotationSessionFactoryBean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 把连接参数信息注入给dataSource --> <property name="dataSource" ref="myDataSource"></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> </props> </property> <!--packagesToScan:自动扫描实体类 (在hibernate中通过<mapping class="cn.mrxia.entity.User"/>指定注解的类),指定则用annotatedClasses属性 <property name="annotatedClasses"> <list><value>cn.mrxia.entity</value></list></property>--> <property name="packagesToScan"> <list> <value>cn.mrxia.entity</value> </list> </property> <!-- 指定hbm.xml映射描述 <property name="mappingResources"> <list> <value>xia/domain/Cost.hbm.xml</value> </list> </property> --> </bean> <!--3 把sessionFactory交给hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 如果要在service层使用事务,需声明事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * cn.mrxia.service.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="exists" read-only="true" /> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- <bean id="demoAction" scope="prototype" class="xia.action.DemoAction"> <property name="costDao" ref="costDAO"></property> </bean> <bean id="costDAO" class="xia.dao.impl.HibernateCostDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> --> </beans>
?