If we Autowire a particular attribute of the class?
would it be considered as a Constructor dependency or a Setter dependency?
If we Autowire a particular attribute of the class?
would it be considered as a Constructor dependency or a Setter dependency?
It depends where you put @Autowired...
1. Put it on a constructor argument then it is a constructor dependency
2. Put it on a method a 'setter' dependency
3. Put it on the field neither of the 2 because now reflection is used to inject the dependencies..
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
Thanks.
So, is my understanding right now?
Autowiring has no relation to Injecting dependencies.
Autowiring is a mechanism that enables connecting classes together.
Dependencies are used to set values in the variables of the classes.
No.. Autowiring has everything to do with injecting dependencies... Connecting classes together IS dependency injection...
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
Thanks again for the response.
So,..
Setter injection mapping :-
<bean id="user" class="com.User" >
<property name="name" value="Eswar" />
<property name="age" value="24"/>
<property name="country" value="India"/>
</bean>
Constructor injection mapping :-
<bean id="user" class="com..User" >
<constructor-arg type="int" value="24"/>
<constructor-arg type="java.lang.String" value="India"/>
</bean>
So, since the mapping is different for both, ideally mapping tells what kind of injection-dependency is followed.
Then I don't understand, how would placing of @Autowiring indicate what kind of mapping is done.
Have you actually READ my post??Then I don't understand, how would placing of @Autowiring indicate what kind of mapping is done.
Originally Posted by mdeinum
Code:public class Foo1 { public Foo(@Autowired Bar bar) {} } public class Foo2 { @Autowired public void setBar(Bar bar) {} } public class Foo3 { @Autowired private Bar bar; }
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
Thanks.
I am still a bit confused as to, why @Autowired will be used, if one can set the dependency without it too.
Anyway, that part, I think I should read the documentation more carefully.
Thanks a ton for the examples and explanation. appreciate it
If you don't use xml to configure your application you need a way to tell what to inject where... This is the case when you use component-scanning (auto detecting of beans and automatically wire the dependencies, hence the name @Autowire )...
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan