2012-03-07 15 views
11

給出一個包含有一個枚舉類:如何在SpEL中引用嵌套類型?

public class MyClass { 
    public enum NestedEnum {   
     value1(1), 
     value2(2); 

     private int code; 

     private NestedEnum(int code) { 
      this.code = code; 
     } 

     public int getCode() { 
      return code; 
     } 
    } 
} 

我如何引用NestedEnum?這:

#{T(MyClass.NestedEnum).value1.getCode()} 

導致異常:

org.springframework.expression.spel.SpelEvaluationException: EL1005E:(pos 0): Type cannot be found 'namespace.MyClass.NestedEnum' 

此:

#{T(T(MyClass).NestedEnum).value1.getCode()} 

導致異常:

org.springframework.expression.spel.SpelParseException: EL1043E:(pos 3): Unexpected token. Expected 'rparen())' but was 'lparen(()' 

我想不出任何其他不錯的選擇嘗試。

回答

18

你必須使用$標誌枚舉分開:

#{T(MyClass$NestedEnum).value1.getCode()} 
+1

它會更好指定'Type'->'NestedEnum'在你的答案的變化,而不是?通常,問題中的「錯誤」代碼應該單獨存在,以便答案中的修復更加明顯。 – sarnold 2012-03-30 02:53:14

+0

這也是一個解決方案,但我認爲這會損害答案的可理解性。我還假設,這是提問者的複製和粘貼錯誤。但是,謝謝你的提示。 – micfra 2012-03-30 07:11:56