【Caused by: java.lang.IllegalArgumentException: Original must not be null 】

本文探讨了在Spring+Struts2+Ibatis项目中遇到的AOP配置问题,详细分析了因使用Cglib代理导致的类型转换错误,并给出了正确的配置方案。

下面是我开发项目中遇到的问题,Caused by: java.lang.IllegalArgumentException: Original must not be null,提示这个错误,百思不得其解。最后面不断的google,spring配置Aop配置相关的文章,最后硬着头皮去看看错误,想调试一番,因为spring的代理无非就是两种,一种是jdk代理;另外一种就是cglib代理。

  在这个项目中我用了spring的cglib代理,但是项目是SSH(spring+struts2+ibatis),struts2  默认注入的方式是jdk模式,问题是否是出在这个地方呢?

  错误:Failed to convert property value of type [com.taobao.www.fanjf.servicei.TaobaokeItemsService$$EnhancerByCGLIB$$20783c4d] to required type

  TaobaokeItemsService(实现类)注入到一个spring受管Bean中, 但是spring容器提示你注入的Bean类型不对,它说你注入的是 [$Proxy13] 类型,这就说明你可能在无意中将authorDaoImp实现了代理, 此时你再在spring容器中引用 TaobaokeItemsService(实现类)时得到的将是代理类型。

  这个似乎提示了我,在struts2当中,我必须通过接口注入方式。

  在com.taobao.www.fanjf.action.DisplayPayedAction这个action当中我必须通过接口注入,最后面确定了,lazy-init="false" 也必须使用

  [html]

  taobaokeItemsManager

  下面是配置:

  [html]

  <!-- 声明一个服务 -->

  <bean id="taobaokeItemsServiceTarget" class="com.taobao.www.fanjf.servicei.TaobaokeItemsService" lazy-init="false" autowire="byType"/>

  <!-- 相关的服务 -->

  <bean  id = "taobaokeItemsManager" class="org.springframework.aop.framework.ProxyFactoryBean" >

  <!--这里代理的是接口-->

  <property name="proxyInterfaces" value="com.taobao.www.fanjf.servicei.ITaobaokeItemsService">

  </property>

  <!--是ProxyFactoryBean要代理的目标类-->

  <property name="target">

  <ref bean="taobaokeItemsServiceTarget"/>

  </property>

  <property name="interceptorNames">

  <list>

  <value>methodCachePointCut</value>

  </list>

  </property>

  <property name="proxyTargetClass" value="true"/>

  </bean>

  <bean id="DisplayPayedAction" class="com.taobao.www.fanjf.action.DisplayPayedAction">

  <property name="taobaokeItemsManager" ref="taobaokeItemsManager"   />

  </bean>

  [java]

  [DEBUG] 2012-12-20 13:28:40 :Unable to apply any optimisations to advised method: protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.aopalliance.aop.Advice)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.springframework.aop.Advisor)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isFrozen()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract org.springframework.aop.TargetSource org.springframework.aop.framework.Advised.getTargetSource()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvisor(org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvisor(int,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setTargetSource(org.springframework.aop.TargetSource)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setPreFiltered(boolean)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isProxyTargetClass()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setExposeProxy(boolean)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isExposeProxy()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract java.lang.Class[] org.springframework.aop.framework.Advised.getProxiedInterfaces()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract org.springframework.aop.Advisor[] org.springframework.aop.framework.Advised.getAdvisors()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isPreFiltered()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isInterfaceProxied(java.lang.Class)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.removeAdvisor(int) throws org.springframework.aop.framework.AopConfigException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.removeAdvisor(org.springframework.aop.Advisor)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.replaceAdvisor(org.springframework.aop.Advisor,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvice(int,org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvice(org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.removeAdvice(org.aopalliance.aop.Advice)

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract java.lang.String org.springframework.aop.framework.Advised.toProxyConfigString()

  [DEBUG] 2012-12-20 13:28:40 :Method is declared on Advised interface: public abstract java.lang.Class org.springframework.aop.TargetClassAware.getTargetClass()

  [DEBUG] 2012-12-20 13:28:40 :Finished creating instance of bean 'fanjfReportMemeberDAO'

  [DEBUG] 2012-12-20 13:28:40 :Creating shared instance of singleton bean 'userInviteDAO'

  [DEBUG] 2012-12-20 13:28:40 :Creating instance of bean 'userInviteDAO'

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'methodCachePointCut'

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'

  [DEBUG] 2012-12-20 13:28:40 :Eagerly caching bean 'userInviteDAO' to allow for resolving potential circular references

  [DEBUG] 2012-12-20 13:28:40 :Getting BeanInfo for class [com.taobao.www.fanjf.daoi.UserInviteDAO]

  [DEBUG] 2012-12-20 13:28:40 :Caching PropertyDescriptors for class [com.taobao.www.fanjf.daoi.UserInviteDAO]

  [DEBUG] 2012-12-20 13:28:40 :Found bean property 'class' of type [java.lang.Class]

  [DEBUG] 2012-12-20 13:28:40 :Found bean property 'dataSource' of type [javax.sql.DataSource]

  [DEBUG] 2012-12-20 13:28:40 :Found bean property 'sqlMapClient' of type [com.ibatis.sqlmap.client.SqlMapClient]

  [DEBUG] 2012-12-20 13:28:40 :Found bean property 'sqlMapClientTemplate' of type [org.springframework.orm.ibatis.SqlMapClientTemplate]

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'sqlMapClient'

  [DEBUG] 2012-12-20 13:28:40 :Invoking afterPropertiesSet() on bean with name 'userInviteDAO'

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'methodCachePointCut'

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'methodCachePointCut'

  [DEBUG] 2012-12-20 13:28:40 :Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'

  [DEBUG] 2012-12-20 13:28:40 :Creating [java.util.concurrent.ConcurrentHashMap]

  [DEBUG] 2012-12-20 13:28:40 :Creating implicit proxy for bean 'userInviteDAO' with 0 common interceptors and 2 specific interceptors

  [DEBUG] 2012-12-20 13:28:40 :Creating CGLIB2 proxy: target source is SingletonTargetSource for target object [com.taobao.www.fanjf.daoi.UserInviteDAO@17e9b52]

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final void org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.setDataSource(javax.sql.DataSource)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final org.springframework.orm.ibatis.SqlMapClientTemplate org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.getSqlMapClientTemplate()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final javax.sql.DataSource org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.getDataSource()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final void org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.setSqlMapClient(com.ibatis.sqlmap.client.SqlMapClient)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final com.ibatis.sqlmap.client.SqlMapClient org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.getSqlMapClient()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final void org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.setSqlMapClientTemplate(org.springframework.orm.ibatis.SqlMapClientTemplate)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [WARN ] 2012-12-20 13:28:40 :Unable to proxy method [public final void org.springframework.dao.support.DaoSupport.afterPropertiesSet() throws java.lang.IllegalArgumentException,org.springframework.beans.factory.BeanInitializationException] because it is final: All calls to this method via a proxy will be routed directly to the proxy.

  [DEBUG] 2012-12-20 13:28:40 :Creating [java.util.concurrent.ConcurrentHashMap]

  [DEBUG] 2012-12-20 13:28:40 :Unable to apply any optimisations to advised method: public java.util.List com.taobao.www.fanjf.daoi.UserInviteDAO.query(java.lang.Long,java.lang.Long,java.lang.String,java.lang.String,java.util.Date,java.util.Date) throws java.lang.RuntimeException

  [DEBUG] 2012-12-20 13:28:40 :Unable to apply any optimisations to advised method: public com.taobao.www.fanjf.pojo.UserInvite com.taobao.www.fanjf.daoi.UserInviteDAO.findUserInviteById(int) throws java.lang.RuntimeException

  [DEBUG] 2012-12-20 13:28:40 :Unable to apply any optimisations to advised method: public void com.taobao.www.fanjf.daoi.UserInviteDAO.saveUserInvite(com.taobao.www.fanjf.pojo.UserInvite) throws java.lang.RuntimeException

  [DEBUG] 2012-12-20 13:28:40 :Unable to apply any optimisations to advised method: public void com.taobao.www.fanjf.daoi.UserInviteDAO.deleteUserInvite(int) throws java.lang.RuntimeException

  [DEBUG] 2012-12-20 13:28:40 :Unable to apply any optimisations to advised method: public void com.taobao.www.fanjf.daoi.UserInviteDAO.updateUserInvite(com.taobao.www.fanjf.pojo.UserInvite) throws java.lang.RuntimeException

更多精彩教程请关注:windows xp系统下载

转载于:https://www.cnblogs.com/qq12345678/p/3247409.html

Java开发中,`java.lang.IllegalArgumentException` 是一个常见的运行时异常,表示传递给方法的参数不合法。当出现 `Filter must not be null` 错误信息时,表明某个方法期望接收到一个非空的 `Filter` 对象,但实际传入了 `null` 值。 ### 异常原因 此异常通常发生在以下几种情况: - 未对输入参数进行非空校验。 - 在某些需要 `Filter` 实例的方法调用中直接传入了 `null`,例如 Spring 或其他框架中的属性拷贝、条件过滤等操作[^1]。 - 开发者自定义的业务逻辑中对参数有严格限制,但未满足条件。 ### 解决方案 #### 1. 参数校验 在调用可能抛出 `IllegalArgumentException` 的方法之前,先对参数进行非空检查。如果参数为 `null`,可以选择跳过该操作或抛出自定义异常以便更清晰地定位问题。 ```java if (filter != null) { // 执行需要 filter 的操作 } else { // 处理 filter 为 null 的情况 logger.warn("Filter is null, skipping operation."); } ``` #### 2. 使用 Optional 类型(Java 8+) 为了减少空指针异常和非法参数异常的发生,可以使用 `Optional` 来包装可能为 `null` 的对象,并通过 `isPresent()` 方法判断是否存在值后再进行后续处理。 ```java Optional<Filter> optionalFilter = Optional.ofNullable(filter); optionalFilter.ifPresent(f -> { // 执行 filter 相关操作 }); ``` #### 3. 默认值设置 如果业务逻辑允许,可以在 `filter` 为 `null` 时提供一个默认实现,避免中断程序执行流。 ```java Filter effectiveFilter = (filter != null) ? filter : new DefaultFilter(); // 继续使用 effectiveFilter ``` #### 4. 日志记录与调试 在捕获到异常后,建议记录详细的日志信息以帮助排查问题根源。可以通过 `try-catch` 捕获异常并输出堆栈跟踪。 ```java try { // 调用可能抛出 IllegalArgumentException 的代码 } catch (IllegalArgumentException e) { logger.error("IllegalArgumentException occurred: ", e); } ``` #### 5. 单元测试验证 编写单元测试来模拟 `filter` 为 `null` 的场景,确保应用程序在这种情况下能够正确响应而不会崩溃。这有助于提高系统的健壮性和可维护性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值