my dog learned polymorphism
The moose likes Spring and the fly likes Spring @Autowired Multiple Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Reply 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
Ranch Hand

Joined: Nov 09, 2011
Posts: 657
    
    2



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: 576

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring @Autowired Multiple Declarations
 
Similar Threads
Is not autowire a another type of dependendcy injection?
Error: Bean property 'userDAO' is not writable or has an invalid setter method.
expected single match bean but found two exception
Issue in annotating DAO using spring 3.0
Not able to Autowire simple class.