Skip to main content
Remove extraneous information for readability.
Source Link
jaco0646
  • 17.4k
  • 10
  • 70
  • 100

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.

I read in some posts about Spring MVC and Portlets 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 it. As i understand it field injection is if you inject a Bean into a attribute with @Autowired like this:

CartController.java:

... @Autowired private Cart cart; ... 

BookshopConfiguartion.java:

@Configuration public class BookShopConfiguration { @Bean public Cart cart(){  return new Cart(); } //more configuration 

My Cart.java is used to store and provide information about the books in the cart.

During my research i read about constructor injection:

MyComponent.java:

... public class MyComponent{ private 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.

I read in some posts about Spring MVC and Portlets that field injection is not recommended. As I understand it, field injection is when you inject a Bean with @Autowired like this:

@Component public class MyComponent {   @Autowired  private Cart cart; } 

During my research I also read about constructor injection:

@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.

Post Reopened by chrylis -cautiouslyoptimistic- java
added 362 characters in body
Source Link
T. Jung
  • 3.7k
  • 3
  • 14
  • 20

I read in some posts about Spring MVC and Portlets 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 it. As i understand it field injection is if you inject a Bean into a attribute with @Autowired like this:

CartController.java:

... @Autowired private Cart cart; ... 

BookshopConfiguartion.java:

@Configuration public class BookShopConfiguration { @Bean public Cart cart(){ return new Cart(); } //more configuration 

My Cart.java is used to store and provide information about the books in the cart.

During my research i read about constructor injection:

MyComponent.java:

... public class MyComponent{ private 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.

I read in some posts about Spring MVC and Portlets 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 it. As i understand it field injection is if you inject a Bean into a attribute with @Autowired like this:

CartController.java:

... @Autowired private Cart cart; ... 

BookshopConfiguartion.java:

@Configuration public class BookShopConfiguration { @Bean public Cart cart(){ return new Cart(); } //more configuration 

My Cart.java is used to store and provide information about the books in the cart.

During my research i read about constructor injection:

MyComponent.java:

... public class MyComponent{ private Cart cart; @Autowired public MyComponent(Cart cart){ this.cart = cart; } ... 

What are the advantages and the disadvantages of both of these types of injections?

I read in some posts about Spring MVC and Portlets 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 it. As i understand it field injection is if you inject a Bean into a attribute with @Autowired like this:

CartController.java:

... @Autowired private Cart cart; ... 

BookshopConfiguartion.java:

@Configuration public class BookShopConfiguration { @Bean public Cart cart(){ return new Cart(); } //more configuration 

My Cart.java is used to store and provide information about the books in the cart.

During my research i read about constructor injection:

MyComponent.java:

... public class MyComponent{ private 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.

Post Closed as "Duplicate" by Software Engineer, Mark Rotteveel java
Source Link
T. Jung
  • 3.7k
  • 3
  • 14
  • 20

What exactly is Field Injection and how to avoid it?

I read in some posts about Spring MVC and Portlets 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 it. As i understand it field injection is if you inject a Bean into a attribute with @Autowired like this:

CartController.java:

... @Autowired private Cart cart; ... 

BookshopConfiguartion.java:

@Configuration public class BookShopConfiguration { @Bean public Cart cart(){ return new Cart(); } //more configuration 

My Cart.java is used to store and provide information about the books in the cart.

During my research i read about constructor injection:

MyComponent.java:

... public class MyComponent{ private Cart cart; @Autowired public MyComponent(Cart cart){ this.cart = cart; } ... 

What are the advantages and the disadvantages of both of these types of injections?