对多个类的依赖

时间:2018-03-04 17:22:21

标签: spring spring-boot dependency-injection

我是Spring的新手,需要公开Web服务以保存客户数据。我的客户对象是具有多个重复元素的巨大json。 我有各自的单独的类,如姓名,地址,身份,特征等 现在,当我为客户定义POJO时,我需要声明所有依赖项,为此优先使用基于构造函数的DI。

我已经阅读了许多文章,声明Class应该承担单一责任,并且对其他类的多个依赖性是设计糟糕的标志 现在我的问题是, 由于我的对象很大,我如何减少这些依赖关系,因为所有这些都是我的根对象客户的一部分。 感谢。

Sample data :
{
   "customer": {
      "partyId": "9000073442",
      "partyRole": [
         {
            "partyRoleId": "CRM000"
         },
         {
            "partyRoleId": "CRM004"
         }
      ],
      "name": [],
      "associationName": [],
      "mobileNumber": "+919387439865",
      "emailId": "ipbl@ril.com",
      "status": "CUSA",
      "IndividualIdentification": [
         {
            "id": "POA",
            "type": "FS9200",
            "number": "23456789",
            "issueDate": "2018-01-04",
            "placeOfIssue": "MUMB",
            "issuingAuthority": "RTO"
         },
         {
            "id": "POI",
            "type": "FS9200",
            "number": "23456789",
            "issueDate": "2018-01-04",
            "placeOfIssue": "MUMB",
            "issuingAuthority": "RTO"
         }
      ],
      "PanIdentification": {
         "number": []
      },
      "characteristics": [
         {
            "attributeName": "DOC_REF_NUM",
            "attributeValue": "EM000000155Q"
         },
         {
            "attributeName": "ROUTING_ZONE",
            "attributeValue": "CO007"
         }
      ],
      "segment": {
         "attributeName": "CUSTOMER_CATEGORY",
         "attributeValue": "0007"
      },
      "associatedPartyId": "9000073441",
      "organization": {
         "name": "IPBillingLocation",
         "type": "0007",
         "designation": [],
         "department": []
      },
      "customerUpdationDateTime": "2018-02-08T12:24:31.776Z",
      "addresses": {
         "validFrom": "2018-01-28T18:30:00Z",
         "addressId": "0001980438",
         "subUnitNr": "23456",
         "buildingName": "sadfgh",
         "streetName": "asdfgh",
         "landmark": "sdfgh",
         "locality": "Ghansoli S.O",
         "postcode": "400701",
         "city": "Mumbai",
         "district": "Thane",
         "state": "MH",
         "country": "IN",
         "jioCenterId": "I001",
         "addressType": "PER_ADD"
      },
      "customerCreationDateTime": "2018-01-29T19:10:05",
      "circleId": "MU",
      "aadharIdentification": {
         "number": []
      }
   },
   "response": {
      "interactionStatus": "0"
   },
   "jioroute": "CO007"
}

我的客户POJO:

public class Customer{

  @Autowired
  private IndividualIdentification Indid;

  @Autowired
  private Characteristics chars;

  @Autowired
  private Addresses  addresses;


  // upto n 
}

这是否符合最佳做法?如果不是减少依赖的替代方法

2 个答案:

答案 0 :(得分:1)

模型类不应该是Spring管理的依赖项 您通常在方法中创建它们,或者在请求反序列化后从WS调用中“接收”它们 从WS调用创建的模型实例是绑定并特定于WS请求的方法 因此将它们注入豆中是没有意义的 使用相当的方法参数通过图层传输它们。

模型类应该是POJO(如果你对JSON和持久层使用相同的类,则应该是JPA实体),而不是Spring bean。

答案 1 :(得分:0)

我认为对于应该做什么课程存在一些误解。

您只需要在服务,控制器,数据访问对象(DAO,与数据库通信),存储库,配置的Spring上下文中注册和注入依赖项。

“Customer”类应该由您实例化并处理。