spring入门(16)--spring常见错误总结 (四)

2014-11-24 10:58:06 · 作者: · 浏览: 4
sdn.spring.advice.SayServiceImpls], but was actually of type [$Proxy4]

这种错误一般出在aop面向切面的编程中,spring面向切面的代理有两种,一种是jdk动态代理,一种是cglib代理;这是你在使用的的使用如果混合时候就会出现上面的错;这两种代理的区别是前者是接口代理,就是返回一个接口类型对象,而后者是类代理,不能返回接口类型对象只能返回类类型对象,如果返回接口了同样会出这样的错。

还有可能出错的地方就是对应的spring配置文件,这

里是最容易马虎出错的地方,仔细检查一下的你的目标对象,比如:

这里在引用bean的时候可能引入错误,可能会引入jdk动态代理的目标类,也有可能你的目标类中实现了某些接口,不符合cglib代理的理念;还有可能马虎出错的地方:

真实对象的id和class属性设置错误的时候也会出错。

12. 错误十二

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxyFactoryBean' defined in class path resource [spring-advice.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'proxyInterfaces' threw exception; nested exception is java.lang.IllegalArgumentException: [www.csdn.spring.proxy.advice.SayServiceImpl] is not an interface

Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'proxyInterfaces' threw exception; nested exception is java.lang.IllegalArgumentException: [www.csdn.spring.proxy.advice.SayServiceImpl] is not an interface

这个问题很好解决,最关键的问题出在红色部分,原因是在spring相关配置文件中设置抽象主题的时候,既然是抽象主题就应该设置成接口,而不应该是实现类。比如下面的代码,注意红色部分:

www.csdn.spring.proxy.advice.SayServiceImpl

正确的写法应该是:

www.csdn.spring.proxy.advice.SayService

13. 错误十三

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxyFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.aop.framework.AopConfigException: Unknown advisor type class www.csdn.spring.proxy.advice.AuditableImpl; Can only include Advisor or Advice type beans in interceptorNames chain except for last entry,which may also be target or TargetSource; nested exception is org.springframework.aop.framework.adapter.UnknownAdviceTypeException: Advice object [www.csdn.spring.proxy.advice.AuditableImpl@1f758500] is neither a supported subinterface of [org.aopalliance.aop.Advice] nor an [org.springframework.aop.Advisor]

Caused by: org.springframework.aop.framework.AopConfigException: Unknown advisor type class www.csdn.spring.proxy.advice.AuditableImpl; Can only include Advisor or Advice type beans in interceptorNames chain except for last entry,which may also be target or TargetSource; nested exception is org.springframework.aop.framework.adapter.UnknownAdviceTypeException: Advice object [www.csdn.spring.proxy.advice.AuditableImpl@1f758500] is neither a supported subinterface of [org.aopalliance.aop.Advice] nor an [org.springframework.aop.Advisor]

这个错误即红色部分提示的错误,不知道advice类型的异常,说白了就是编写的spring通知的错误,这种错误比较常见,也是出于马虎的错误,比如前者通知、后置通知、环绕通知、异常通知、引入通知等这几个通知中的任何一个通知类忘了写继承的父类;以下列出这几个通知的类编写所继承的类:

前置通知:

public class BeforeAdvice implements MethodBeforeAdvice

后置通知:

public class AfterAdvice implements AfterReturningAdvice

环绕通知:

public class AroundAdvice implements MethodInterceptor

异常通知:

public class ThrowAdvice implements ThrowsAdvice

引入通知:

public class AuditableImpl extends Delegating