今天在开发个人博客时,用到了aop思想,主要体现在事务管理上.但由于编程经验不是很足,搞了整整一个下午才装注册和登陆系统粗粗的做好.时间主要浪费在了调试上.
下面把我今天错误的地方跟在家说一说,相信可能对你有所帮助.
下面把我的applicationContext.xml文件先放出来.
<?xml version="1.0" encoding="GB18030"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="userDao" class="loginreg.dao.UserDaoHibernater"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="mgr" class="loginreg.bussiness.UserManagerImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 配置使用hibernate局部事务策略 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 定义事务拦截器 --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 定义BeanNameAutoProxyCreator--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>mgr</value> </list> </property> <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> </beans>
以上配置是正确的,但是在我发现错误之前,始终会出现这样的错误:
写道
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionInterceptor' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'transactionAttributes' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.util.StringUtils.containsWhitespace(Ljava/lang/String;)Z
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property 'transactionAttributes' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.util.StringUtils.containsWhitespace(Ljava/lang/String;)Z
Caused by: java.lang.NoSuchMethodError: org.springframework.util.StringUtils.containsWhitespace(Ljava/lang/String;)Z
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionInterceptor' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'transactionAttributes' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.util.StringUtils.containsWhitespace(Ljava/lang/String;)Z
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property 'transactionAttributes' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.util.StringUtils.containsWhitespace(Ljava/lang/String;)Z
Caused by: java.lang.NoSuchMethodError: org.springframework.util.StringUtils.containsWhitespace(Ljava/lang/String;)Z
稍微看一下,就会发现是说少了一个方法.我随着这个方法进入spring.jar源文件,找到这个方法,的确没发现有关的方法.我查了很多文档,配置没有任何错误.当时一直没有想到spring.jar版本过低,因为我用的是最新版的myeclipse,后来在网业上偶尔看了几行字,受到了启发,我下了最新版的spring.jar.2.5版本的,结果调试成功.