【错误记录】Groovy 注入方法报错 ( Cannot add new method [hello] for arguments [[]]. It already exists )
生活随笔
收集整理的這篇文章主要介紹了
【错误记录】Groovy 注入方法报错 ( Cannot add new method [hello] for arguments [[]]. It already exists )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、報錯信息
- 二、解決方案
一、報錯信息
使用 MetaClass 為類注入方法時 , 如果注入的方法與類中原有的方法有沖突 ,
class Student {def name;def hello() {println "Hello " + name} }// 向 Student 類注入 hello 方法 Student.metaClass.hello << {println delegateprintln "Hello ${delegate.name}" }執行上述方法 , 會有如下報錯 ;
報錯信息 :
Caught: groovy.lang.GroovyRuntimeException: Cannot add new method [hello] for arguments [[]]. It already exists! groovy.lang.GroovyRuntimeException: Cannot add new method [hello] for arguments [[]]. It already exists!at Groovy.run(Groovy.groovy:11)二、解決方案
如果使用 Category 分類的方式注入方法 , 注入的方法可以與類中原來的方法相同 , 參考 【Groovy】MOP 元對象協議與元編程 ( 方法注入 | 使用 @Category 注解進行方法注入 | 分類注入方法查找優先級 ) 博客 ;
使用 MetaClass 的方式注入方法 , 注入的方法不可與原來的方法沖突 , 否則就會報上述錯誤 ; 參考 【Groovy】MOP 元對象協議與元編程 ( 方法注入 | 使用 MetaClass 進行方法注入普通方法 ) 博客 ;
總結
以上是生活随笔為你收集整理的【错误记录】Groovy 注入方法报错 ( Cannot add new method [hello] for arguments [[]]. It already exists )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】MOP 元对象协议与元编
- 下一篇: 【Groovy】MOP 元对象协议与元编