I read in some posts about Spring MVCSpring MVC and PortletsPortlets that field injection is not recommended. Cause i'm trying to get a So i asked myself if i'm using field injection and i can't answer itis not recommended. As iI understand it, field injection is ifwhen you inject a Bean into a attributeBean with @Autowired like this:
CartController.java:
... @Autowired private Cart cart; ... BookshopConfiguartion.java:
@Configuration@Component public class BookShopConfigurationMyComponent { @Bean public Cart cart(){ @Autowired return newprivate Cart(); cart; } //more configuration My Cart.java is used to store and provide information about the books in the cart.
During my research iI also read about constructor injection:
MyComponent.java:
...@Component public class MyComponent { private final Cart cart; @Autowired public MyComponent(Cart cart){ this.cart = cart; } ...} What are the advantages and the disadvantages of both of these types of injections?
EDIT 1: As this question is marked as duplicate of this question i checked it. Cause there aren't any code examples neither in the question nor in the answers it's not clear to me if i'm correct with my guess which injection type i'm using.