懒得笔记5 spring aop 整合hibernate 事务管理


1,在xml 中加上

          a) 加上对应的xsd文件spring-aop.xsd 

             xmlns:aop="http://www.springframework.org/schema/aop"

            http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"

           b) beans.xml <aop:aspectj-autoproxy />   // 表明自动产生代理


2,

      

       后面三个包。。不然各种错。。不知道要怎么样才知道要导入哪种包。

3,annotation 和 xml 两种方式 

 xml要掌握

4,spring 整合 hibernate

    a, 引入 data common 包  hibernate 等。。。根据报的错去导入

    b, 生成sessionFactory

            spring 整合 hibernate使用annotation 

          

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" /> <!-- sessionFactory 织入对象 -->
		<property name="annotatedClasses"> <!-- 指明数据对象 -->
			<list>
				<value>com.model.User</value>
				<value>com.model.Log</value>
			</list>
		</property>
		<property name="hibernateProperties"> <!-- 可以设置若干属性 -->
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

声明式的事务管理

5, 声明式的事务管理   实现在DAO这一层

    a, 加入 

xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
       http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd

  b, 加入 

     <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

c , 在事务的地方加上

  @Transactional(readOnly=true) 如

  

@Transactional 
	public void add(User user) {
		
			userDAO.save(user);
			Log log = new Log();
			log.setMsg("a user saved!");
			logDAO.save(log);
		
	}
例子    点击打开链接

  使用xml

<?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.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-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com" />
	 
	 

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<context:property-placeholder location="jdbc.properties" />


     <bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" /> <!-- sessionFactory 织入对象 -->
		<property name="annotatedClasses"> <!-- 指明数据对象 -->
			<list>
				<value>com.model.User</value>
				<value>com.model.Log</value>
			</list>
		</property>
		<property name="hibernateProperties"> <!-- 可以设置若干属性 -->
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	 
	 <bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	
	 

     
    <tx:advice id="txAdvice" transaction-manager="txManager">     
        <tx:attributes>          
            <tx:method name="get*" read-only="true"/>      
            <tx:method name="add*"/>
        </tx:attributes>
    </tx:advice>

 
    <aop:config>
        <aop:pointcut id="bussinessService" expression="execution(* com.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService"/>
    </aop:config>
	
</beans>


packagetoscan


猜你喜欢

转载自blog.csdn.net/chen_xinjia/article/details/47668493