File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring @Autowired Multiple Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring @Autowired Multiple Declarations" Watch "Spring @Autowired Multiple Declarations" New topic
Author

Spring @Autowired Multiple Declarations

Sreenivas Reddy Tatikunta
Greenhorn

Joined: Aug 23, 2011
Posts: 24

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

Joined: Aug 23, 2011
Posts: 24

Please help me out .. this is urgent requirement




Regards,
Sreenivas Reddy.Tatikunta
Dattatraya Tembare
Greenhorn

Joined: Feb 01, 2009
Posts: 10
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

Joined: Nov 09, 2011
Posts: 1012
    
    5



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

Joined: Feb 08, 2009
Posts: 597

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


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Spring @Autowired Multiple Declarations
 
Similar Threads
expected single match bean but found two exception
Issue in annotating DAO using spring 3.0
Not able to Autowire simple class.
Error: Bean property 'userDAO' is not writable or has an invalid setter method.
Is not autowire a another type of dependendcy injection?