Announcement Announcement Module
Collapse
No announcement yet.
@Autowiring doubt Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowiring doubt

    If we Autowire a particular attribute of the class?

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

  • #2
    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..

    Comment


    • #3
      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.

      Comment


      • #4
        No.. Autowiring has everything to do with injecting dependencies... Connecting classes together IS dependency injection...

        Comment


        • #5
          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.

          Comment


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

            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;
            }

            Comment


            • #7
              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

              Comment


              • #8
                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 )...

                Comment

                Working...
                X