Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring @Autowired Multiple Declarations

 
Sreenivas Reddy Tatikunta
Greenhorn
Posts: 24
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Please suggest me, can i use @Autowired in the following ways.

@Autowired
private A a;
private B b;

(or)

@Autowired
private A a;

@Autowired
private B b;

Thanks in Advance

Best Regards,
Sreenivas Reddy.Tatikunta
 
Sreenivas Reddy Tatikunta
Greenhorn
Posts: 24
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me out .. this is urgent requirement




Regards,
Sreenivas Reddy.Tatikunta
 
Dattatraya Tembare
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure about second approach; it will work perfectly but not sure about performance point of view which is better.
@Autowired
private A a;

@Autowired
private B b;

For now you can start you work ....

Go though the Spring documentation there are several ways to use it.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You may think this style works but if you try it, you will find that b is not autowired.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:

You may think this style works but if you try it, you will find that b is not autowired.


Nor should it work. Annotation apply only to immediate elements. It would be very confusing if annotations started to cascade. If it did, we'll need a @StopAutowired
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic