声明注解式事务

参考错误解决:
终于找到全annotation配置springMVC的方法了(事务不失效): http://icanfly.iteye.com/blog/778401
解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题:
http://fengzhiyin.iteye.com/blog/714686
留言下面写到: mvc 的只扫描controller组件 注意使用 use-default-filters="false"
<context:component-scan base-package="com.fengzhiyin" use-default-filters="false" >
<context: include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

主体的扫描除controller外的所有组件
<context:component-scan base-package="com.fengzhiyin" >
<context: exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

产生问题:
java.lang.NoClassDefFoundError: org/objectweb/asm/Type:
http://blog.csdn.net/apple90/article/details/2968658
java.lang.NoSuchMethodError: org.objectweb.asm.org.objectweb.asm.ClassWriter.:
http://wxpwdm8461.iteye.com/blog/586575


同时, 抛出异常应该是RuntimeException类型的异常,否则事务失败。
throw new RuntimeException("@@@@@@@@@@@@@@@@@@ RollBack...............");


配置原则:
spring-servlet.xml只负责扫描controller类。
applicationContext.负责扫描controller以外的类。

因为spring的context是父子容器,所以会产生冲突,Controller会先进行扫描装配,而此时的Service还没有进行事务的增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描配置进行事务处理。
xml被扫描顺序参考http://sence-qi.iteye.com/blog/1328902
由于服务器启动时的加载配置文件的顺序为web.xml---root-context.xml(Spring的配置文件)---servlet-context.xml(SpringMVC的配置文件),由于root-context.xml配置文件中Controller会先进行扫描装配,但是此时service还没有进行事务增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力)

配置文件:
applicationContext.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:tx="http://www.springframework.org/schema/tx" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans       
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       
    http://www.springframework.org/schema/context       
    http://www.springframework.org/schema/context/spring-context-3.0.xsd    
    http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
    http://www.springframework.org/schema/tx       
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       
    http://www.springframework.org/schema/jdbc       
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
    default-autowire="byName" default-lazy-init="true">

	<aop:aspectj-autoproxy proxy-target-class="true"/> 
<!-- 	<context:component-scan base-package="com.">  
	    <context:exclude-filter type="regex" expression=".*Controller$" />  
	</context:component-scan>  -->
	
	<context:component-scan base-package="com" > 
		<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
	</context:component-scan> 
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver">
		</property>
		<property name="url"
			value="jdbc:sqlserver://localhost:1433;DatabaseName=UNIT_TEST_CASE_SYS">
		</property>
		<property name="username" value="sa"></property>
		<property name="password" value="asl12345"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					com.common.SqlServer2008Dialect
				</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.pojo.Modules</value>
				<value>com.pojo.TestCaseDetails</value>
				<value>com.pojo.RoundDetails</value>
				<value>com.pojo.Projects</value>
				<value>com.pojo.Users</value>
				<value>com.pojo.TestCases</value>
				<value>com.pojo.Functions</value>
				<value>com.pojo.UserType</value>
				<value>com.pojo.Rounds</value>
				<value>com.pojo.ProjectBrowers</value>
			</list>
		</property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</bean>
</beans>


<property name="annotatedClasses">
			<list>
				<value>com.pojo.Modules</value>
				<value>com.pojo.TestCaseDetails</value>
				<value>com.pojo.RoundDetails</value>
				<value>com.pojo.Projects</value>
				<value>com.pojo.Users</value>
				<value>com.pojo.TestCases</value>
				<value>com.pojo.Functions</value>
				<value>com.pojo.UserType</value>
				<value>com.pojo.Rounds</value>
				<value>com.pojo.ProjectBrowers</value>
			</list>
		</property>

可以让
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
		p:packagesToScan="com.pojo" >

去配置,就不用每个都写一句。


spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation=" http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc    
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


	<mvc:annotation-driven />
<!-- 	<context:component-scan base-package="com.service.impl" />
	<context:component-scan base-package="com.dao.impl" />
	<context:component-scan base-package="com.pojo" />
	<context:component-scan base-package="com.controller" /> -->
	<context:component-scan base-package="com" use-default-filters="false" > 
		<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
	</context:component-scan> 

	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	

</beans>

你可能感兴趣的:(注解)