2012-01-27 79 views
2

我遇到了問題,我希望有人能幫助我。Spring - AOP - 註釋代碼生成

我使用Spring框架,我想裏面生成我的域名類的一個方法...

我有一個域實體:

@Entity 
class AbcDomain { 
    private int id; 

    @CustomAnnotation(p1="x1", p2="y1") 
    private String a; 

    @CustomAnnotation(p1="x2", p2="y2") 
    private String b; 

    @CustomAnnotation(p1="3", p2="y3") 
    private int c; 

    private Set<Integer> d; 
    ... getters and setters 
} 

我要生成「 m「方法到這個域。 方法包含的Fileds,這得到了「CustomAnnotation」的註釋是這樣的:

@Entity 
class AbcDomain { 
    ... Prev code ... 

    public String m() { 
     if ("x1".equals(a)) // "x1" comes from the annotation's p1 param 
      return "y1";  // "y1" comes from the annotation's p2 param 

     if ("x2".equals(b)) // "x2" comes from the annotation's p1 param 
      return "y2";  // "y2" comes from the annotation's p2 param 

     if (c == 3) // 3 comes from the annotation's p1 param 
      return "y3"; // "y3" comes from the annotation's p1 param 
    } 
} 

我很新的春天和AOP,任何人可以幫助我在哪裏可以找到解決辦法嗎?

感謝

回答

0

你需要編譯時的AspectJ那麼你可以做一些所謂Inter-Type-Declaration(此的技術方法在Spring Roo的大量使用,所以如果你想有一個完整的例子創建Spring Roo的項目)。

privileged aspect AbcDomain_Extension { 

    public String m() { 
     if ("x1".equals(a)) // "x1" comes from the annotation's p1 param 
      return "y1";  // "y1" comes from the annotation's p2 param 

     if ("x2".equals(b)) // "x2" comes from the annotation's p1 param 
      return "y2";  // "y2" comes from the annotation's p2 param 

     if (c == 3) // 3 comes from the annotation's p1 param 
      return "y3"; // "y3" comes from the annotation's p1 param 
    } 

}