0

I deserialize a json and want then to call postProcess method by annotation or another way after constractor?

json file:

{
  "type": "storeType",
  "name": "store-name",
  "list": [
    {
      "type": "itemType",
      "name": "item1-name",
    },
    {
      "type": "itemType",
      "name": "item2-name",
    }
  ]
}

Store.class:

 Store extends AbstractClass{

    @Value("${store.size:100000}")
    private Integer size;
    @Autowired
    private StorePersistency persistency;

    private String name;
    private List<abstractClass> list;

    public Store(@JsonProperty("name") String name, @JsonProperty("list") list) {
        this.name=name;
        this.list=list;
    }

   @Override
   public postProcess(){
     ...
   }
}

Item.class:

 Item extends AbstractClass{

    private String name;

    public Store(@JsonProperty("name") String name) {
        this.name=name;
    }

   @Override
   public postProcess(){
     ...
   }
}

deserializerService:

AbstractClass clzz = objectMapper.readValue(jsonFile, AbstractClass.class)

The purpose is to @authowire and @value other class fields (StorePersistency and size) by calling a authowireService in postProcess method.

AuthowireService:

public void authowireBean(Class clzz){
   applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(clzz, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
}
Maria Dorohin
  • 355
  • 4
  • 17
  • you should just create own json deserializer for such class, to get rid of all the other beans from it too. – GotoFinal Sep 03 '19 at 07:42

1 Answers1

0

You should definitely decouple POJO from persistence and service layers. For persistence layer you could use DAO pattern which is probably represented by StorePersistency class in your case. You need to create service layer which implement postProcess. See Responsibilities and use of Service and DAO Layers for more info. Example service layer in your case could look like below:

@Component
class StoreService {

    @Value("${store.size:100000}")
    private Integer size;

    @Autowired
    private StorePersistency persistency;

    public postProcess(Store store) {
       ...
    }
}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • I need to use the StorePersistency and size and maybe more other beans (that not related to store) at the same Store.class, which I use for json deserialization.. – Maria Dorohin Sep 03 '19 at 07:04
  • @MariaDorohin, if beans are not related with `Store` class they should not be injected to `Store` class. Probably you have some actions to implement which do something with `Store`. You can implement [Command Pattern](https://www.baeldung.com/java-command-pattern) and create many `CommandStore1`, `CommandStore2` and inject whatever you need there. – Michał Ziober Sep 03 '19 at 11:44