Results 1 to 8 of 8

Thread: @Autowiring doubt

  1. #1
    Join Date
    Oct 2010
    Posts
    5

    Default @Autowiring doubt

    If we Autowire a particular attribute of the class?

    would it be considered as a Constructor dependency or a Setter dependency?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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

  5. #5
    Join Date
    Oct 2010
    Posts
    5

    Default

    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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Then I don't understand, how would placing of @Autowiring indicate what kind of mapping is done.
    Have you actually READ my post??

    Quote Originally Posted by mdeinum
    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..
    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

  7. #7
    Join Date
    Oct 2010
    Posts
    5

    Default

    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

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •