1. 程式人生 > >Spring AOP代理類生成錯誤

Spring AOP代理類生成錯誤

在整合的ssh時出現一個Spring  AOP代理類生成的錯誤,問題出現在配置事務的時候,切面表示式寫的範圍太大,導致執行Spring事務時候生成代理類時出現了一個錯誤

專案目錄

配置檔案

    <!-- 匯入外部檔案 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    
    <context:component-scan base-package="zhuojing.ssh"></context:component-scan>
    
    
    <!-- 配置資料來源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="user" value="${jdbc.username}"></property>
       <property name="password" value="${jdbc.password}"></property>
       <property name="driverClass" value="${jdbc.classDriver}"></property>
       <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 
       <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
       <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
       
    </bean>


   <!-- 配置SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
		<property name="mappingLocations" value="classpath:zhuojing/ssh/entites/*.hbm.xml"></property>
	</bean>
	
	<!-- 配置宣告式事務 -->
	<!-- 配置事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<!-- 配置事務屬性 -->
	<tx:advice transaction-manager="transactionManager" id="txAdvice">
	   <tx:attributes>
          <tx:method name="get*" read-only="true"/>
          <tx:method name="lastNameRigntOr" read-only="true"/>
          <tx:method name="*"/>
	   </tx:attributes>
	</tx:advice>
	
	<!-- 配置切點 -->
	<aop:config>
	    <aop:pointcut expression="execution(* zhuojing.ssh.service.*.*(..))" id="txPoint"/>
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
	    
	</aop:config>

出現的錯誤:

java.lang.NoSuchMethodException: com.sun.proxy.$Proxy19.list()
	ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1246)
	ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
	com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
	com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
	ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
	ognl.ASTMethod.getValueBody(ASTMethod.java:90)
	ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
	ognl.SimpleNode.getValue(SimpleNode.java:258)
	ognl.Ognl.getValue(Ognl.java:494)
	ognl.Ognl.getValue(Ognl.java:458)
	com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309)
	com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
	com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:307)
	com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423)
	com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250)
	com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
	org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
	org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)

分析錯誤:

Spring 生成代理物件方式有兩種

1:利用jdk中的proxy實現,要求我們的被代理物件必須要去實現一個代理介面,代理物件和被代理物件本質是是實現了統一介面的兩個物件(預設方式)

2:利用cglib來實現.被代理物件不需要去實現一個代理介面,被代理類和代理類之間本質是父子類的關係

proxy-target-class="true" 是指定由cglib來實現實現代理.

而在上面配置的方式中切點表示式設為execution(* zhuojing.ssh.*.*.*(..))則Action也會在事務的切面中,並且Action並有實現一個代理介面,
 當使用jdk生成代理物件時會出現ava.lang.NoSuchMethodException: com.sun.proxy.$Proxy42.list()的錯誤,

解決方式:

1.加入:<aop:aspectj-autoproxy proxy-target-class="true" />  利用cglib來生成代理物件

2.修改切點表示式execution(* zhuojing.ssh.service.*.*(..))