对多个类的依赖性

问题描述 投票:0回答:2

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

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

Sample data :
{
   "customer": {
      "partyId": "9000073442",
      "partyRole": [
         {
            "partyRoleId": "CRM000"
         },
         {
            "partyRoleId": "CRM004"
         }
      ],
      "name": [],
      "associationName": [],
      "mobileNumber": "+919387439865",
      "emailId": "[email protected]",
      "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 
}

这符合最佳做法吗?如果没有,那么减少依赖的替代方案是什么

spring spring-boot dependency-injection
2个回答
1
投票

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

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


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.