• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Beans creation using annotation

 
Ranch Hand
Posts: 654
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have three confusions while bean creation using annotations:
1. How is bean injected when a member variable is @Autowired ?
2. When I Autowired all three ways possible (i.e Member variable, setter & constructor), how the bean is injected(in my code the customerRepo bean)
3. I ran the code and go logs as below:
constructor injection
setter injection

why is constructor injection called first?



 
author & internet detective
Posts: 40521
825
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3) A constructor is run when an object is instantiated. This means the only opportunity for a constructor to run is before the object is ready for use by other methods. Which means the constructor injection needs to happen before the setter.

2) Try it? The intent is to do all three on the same field

1) I don't understand the question. Spring injects it for you.
 
Puspender Tanwar
Ranch Hand
Posts: 654
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:3) A constructor is run when an object is instantiated. This means the only opportunity for a constructor to run is before the object is ready for use by other methods. Which means the constructor injection needs to happen before the setter.

Thanks, lots of things were going throw my mind and I skipped this simple concept.

Jeanne Boyarsky wrote:2) Try it? The intent is to do all three on the same field

Yes I tried it and intent is to do all three on the same field, actually just for better understanding of bean injection, not in general programming.

Jeanne Boyarsky wrote:1) I don't understand the question. Spring injects it for you.

My question was: like setter autowire inject bean using a setter method, constructor autowire uses a constructor then how the bean is injected in case of member variable autowiring? I hope now my question makes sense.
 
Puspender Tanwar
Ranch Hand
Posts: 654
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if someone having answers to my doubts, please respond.
 
Saloon Keeper
Posts: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spring injects dependencies into fields that are annotated with @Autowired using reflection, likely using the methods of the java.lang.reflect.Field class.
 
Bartender
Posts: 1087
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add (in IMHO) its best not to use field injection as it makes writing test cases awkward.    
 
Stephan van Hulst
Saloon Keeper
Posts: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Peter. You should pretty much always prefer constructor injection.
 
Puspender Tanwar
Ranch Hand
Posts: 654
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I agree with Peter. You should pretty much always prefer constructor injection.


but there are some posts that advise using setter injection over constructor injection.
 
Author
Posts: 133
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I agree with Peter. you should pretty much always prefer constructor injection.



Just to add (in IMHO) its best not to use field injection as it makes writing test cases awkward.  




How so, regarding tests?

To be honest with you, I'm actually seeing this as a completely religious issue. I've worked with several injection styles over the years, and from a pragmatic viewpoint.......i really don't care
 
Stephan van Hulst
Saloon Keeper
Posts: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it's difficult to unit test your services if you can't inject mock dependencies into them. There aren't many nice mocking frameworks that allow you to inject dependencies straight into private fields.
 
Stephan van Hulst
Saloon Keeper
Posts: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:but there are some posts that advise using setter injection over constructor injection.


Please show us where you read that.
 
Peter Rooke
Bartender
Posts: 1087
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using field injection you have tightly coupled your POJO code to the (Spring) DI framework.  Unit testing should really not involve the DI container, that's the purpose of integration tests.
So when you write unit tests, in the absence of the framework, your going to have to monkey around with reflection to inject the values.  


Injection
Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies. For more background on why field based injection is evil, you can read this article by Oliver Gierke.
As always there is one exception to the rule, it’s fine to use field-based injection in tests when you’re using the SpringJUnit4ClassRunner.
.....
Testing
The golden rule for unit testing in Spring is keep Spring out of your unit testing! It should be possible to unit test the majority of your beans without needing to spin up an application context.

How not to hate Spring 2016

Feild DI considered harmful  
 
Puspender Tanwar
Ranch Hand
Posts: 654
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Puspender Tanwar wrote:but there are some posts that advise using setter injection over constructor injection.


Please show us where you read that.


I actually don't remember where I read that article. If I found that, definitely I will share that here.

Anyways, from this thread, I get to know that field injection are evil and we shouldn't use them.
 
What's brown and sticky? ... a stick. Or a tiny ad.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic