使用注释(使用AOP?)将字段(带有getter和setter)注入POJO

时间:2014-02-01 10:23:11

标签: java annotations pojo aop

是否可以使用带注释的字段和getter + setter来装饰POJO?假设我有一个类似已编辑

的课程
public abstract class Human { //to be extended by classes like Male, Female
    private String name;
    private String identificationNumber;
    private String mailingAddress;

    public void setName(String name) {...}
    public String getName() {...}

    public void setIdentificationNumber(String number) {...}
    public String getIdentificationNumber() {...}

    public void setMailingAddress(String address) {...}
    public String getMailingAddress() {...}
}

我还有另一个类,如已编辑

public abstract class Animal { //to be extended by classes like Dog, or Wolf
    private String name;
    private String furColor;
    private String ferocityIndex;

    public void setName(String name) {...}
    public String getName() {...}

    public void setFurColor(String color) {...}
    public String getFurColor() {...}

    public void setFerocityIndex(String index) {...}
    public String getFerocityIndex() {...}
}

如果我需要创建一个狼人(人类和动物的混合体)的场景,基本上我需要“扩展”这两个类。我已经明白我可以为两个抽象类使用接口,但这意味着重新实现getter和setter,并将字段实现复制到所有实现类。

我希望能够将Human和Animal定义为注释,因此Werewolf类可以看起来像:

@Human
@Animal
public class Werewolf {
}

我应该能够看到主类中的getter和setter,如:

public static void main(String[] args) {
    Werewolf jonathan = new Werewolf();
    jonathan.getName();
    //should be Werewolf's not Human's implementation or Animal implementations
    //if jonathan has a different animal name, @Animal should implement animalName, not name
    jonathan.getFerocityIndex(); //should be here
    jonathan.getIdentificationNumber(); //should also be here
}

我的项目目前使用spring,所以只要有人可以指出我正确的方向,或者已经有一个库,那么Spring AOP就不是不可能的了。因为我从某个角度思考,狼人有人的方面和动物方面,CMIIW。

简而言之:在编译期间,我是否有注入一组带有getter + setter(由注释定义?)的字段到注释类中?

修改了更多详情

  • @Animal@Human都定义了name字段。注入Werewolf时应该成为他的(这是?)name
  • 来自user2693476的答案提供IHumanIAnimal的组合将使name字段不一致,是人类还是动物?
  • 我的目标是让@Human@Animal告诉带注释的Werewolf课程: 我特此将这些字段作为您自己的 即可。所以,在某种程度上:代码模板。使用反射,我可以检测name的重复字段并仅应用一次,或抛出RuntimeException。但我还不知道如何将代码注入Werewolf类。

-Jonathan

2 个答案:

答案 0 :(得分:0)

Spring AOP的目的是注入一些在许多方法中重复的公共代码,这样你只需要在方法中定义那些公共代码,你就可以在你的程序中的任何地方使用它,这不是适合您的问题。 在你的情况下,这看起来更像是OO设计问题,因为你的狼人需要分享人类和动物的属性,你可以为人类和动物定义另外两个界面:

public interface IHuman{ //implemented by Human and Werewolf
  public void setIdentificationNumber(String number);
  public String getIdentificationNumber();
  public void setMailingAddress(String address);
  public String getMailingAddress();
}

public interface IAnimal { //implemented by Animal and Werewolf
  public void setFurColor(String color);
  public String getFurColor() {...}

  public void setFerocityIndex(String index);
  public String getFerocityIndex();
}

在你的狼人课程中,你可以利用作文的好处来帮助你实现自己的想法。

public class Werewolf implements IHuman, IAnimal{
    private IHuman human;
    private IAnimal animal;

    //create getter and setter for human and animal to assign the concrete implemented Human    and Animal instance to Werewolf
    ...
  //and you can define all your unimplemented methods in this way
  public void setIdentificationNumber(String number){
      this.human.setIdentificationNumber(number);
  }

  public String getIdentificationNumber(){
      return this.human.getIdentificationNumber();
  }

  public void setMailingAddress(String address){
      this.human.setMailingAddress(address);
  }

  public String getMailingAddress(){
      return this.human.getMailingAddress();
  }

  public void setFurColor(String color){
      this.animal.setFurColor(color);
  }
  public String getFurColor() {
      return this.animal.getFurColor();
  }

  public void setFerocityIndex(String index){
     this.animal.setFerocityIndex(index);
  }
  public String getFerocityIndex(){
     return this.animal.getFerocityIndex();
  }

}

答案 1 :(得分:0)

您似乎在使用注释寻找Java中的多重继承的解决方法。检查Lombock项目的@Delegate注释。如果您不喜欢它,您应该查看Java的注释处理工具并编写您自己的注释。请注意此类解决方案的可能缺点(IDE支持,文档,新贡献者的可读性......)。