-2

Im not sure about approach to initialize and implement specific part of spring boot app.

I got e-commerce app that got Purchase class with fields (not sure whether its called field- eg "private String name;") and getters and setters. one of the field is shippingAddress address - own class with constructor with its fields.

My question is could I use:

Purchase purchase = context.getBean(Purchase.class);
purchase.setName("foo0");
purchase.setShippingAddress(new ShippingAddress("foo1", "foo2", "foo3"))

?(check last line). I mean, is going via "new" good approach? @Autowired annotation keeps popping into my mind but I think its not suitable here?

  • question has already been addressed in this forum. https://stackoverflow.com/questions/24213823/anyway-to-inject-autowire-an-inner-class-into-an-outer-class – Superchamp Jul 03 '21 at 03:38

1 Answers1

0

The ShippingAddress should be set for each user from input, not injected on startup by Spring. new is appropriate here; @Autowired is not.

duffymo
  • 305,152
  • 44
  • 369
  • 561