11

Maybe, because of my wrong English, I couldn't understand the benefit of using @Autowired annotation.

According to the tutorial we can simplify the first(I.) case to second case(II.) by means of @Autowired.

My question is, what is the meaning of the @Autowired ? Because it doesnt tell any more, since without using @Autowired the compiler can figure out that "EmpDao emDao" and "EmpManager" are closely related according the declaration.

code cited from here

I.

    <bean id="empDao" class="EmpDao" />
    <bean id="empManager" class="EmpManager">
       <property name="empDao" ref="empDao" />
    </bean>

public class EmpManager {

   private EmpDao empDao;

   public EmpDao getEmpDao() {
      return empDao;
   }

   public void setEmpDao(EmpDao empDao) {
      this.empDao = empDao;
   }

   ...
}

II.

<context:annotation-config />

<bean id="empManager" class="autowiredexample.EmpManager" />
<bean id="empDao"     class="autowiredexample.EmpDao" />

import org.springframework.beans.factory.annotation.Autowired;

public class EmpManager {

   @Autowired
   private EmpDao empDao;

}
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
cscsaba
  • 1,279
  • 3
  • 20
  • 31
  • 2
    I don't think I can explain it any clearer than your example already does. The second case is shorter and has less code clutter - why isn't that a good thing in and off itself? – skaffman Feb 14 '11 at 22:10
  • I see we can save some line, but I dont understand real benefit of the @Autowired. if Spring makes instance of EmpManager than it will make an EmpDao empDao as well in the same time. With or without @Autowired we have empDao as well – cscsaba Feb 14 '11 at 22:42
  • Possible duplicate of [Understanding Spring @Autowired usage](https://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage) – tkruse Dec 06 '17 at 05:10

4 Answers4

11

@Autowired is spring-specific. @Inject is the standard equivallent. It is an annotation that tells the context (spring, or in the case of @Inject - any DI framework) to try to set an object into that field.

The compiler has nothing to do with this - it is the DI framework (spring) that instantiates your objects at runtime, and then sets their dependencies at the points you have specified - either via XML or via an annotation.

I agree it is a possible scenario for a DI framework to try to inject dependencies into all fields, even if they are not annotated. (And if you want to exclude a particular field, to annotate it). But they chose the other strategy (configuration-over-convention). By the way:

  • if using xml config and choose some form of autowiring, the dependencies of the bean will be automatically autowired without the need to specify anything
  • you can specify per-context autowiring settings.
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I got it, so the Spring don't know anything about declaration (compiler like features), that's why we have to lead the Spring how the classes are related. Right ? – cscsaba Feb 14 '11 at 22:19
  • @user303352 spring knows all about classes, but it doesn't know whether you _want_ it to inject something there or not. – Bozho Feb 14 '11 at 22:25
  • But if the Spring makes instance of EmpManager than it will make an EmpDao empDao as well in the same time. With or without @Autowired we have empDao as well – cscsaba Feb 14 '11 at 22:33
  • 1
    @user303352 you have it, but you may not want to inject it there (I don't say it's a common case). It could've been the default strategy to assume the presense of `@Autowired`, but it they didn't choose it that way. – Bozho Feb 14 '11 at 22:46
  • As I know every classes in appcontext.xml are about injection, briefly Spring uses our classes to instantiation. I dont see any plus option, if it is in the appcontext.xml than it will be used ie will be injected. – cscsaba Feb 14 '11 at 22:55
  • back to "I agree it is a possible scenario for a DI framework to try to inject dependencies into all fields, even if they are not annotated. (And if you want to exclude a particular field, to annotate it). But they chose the other strategy (configuration-over-convention)" without @Autowired we have empDao which is null ? – cscsaba Feb 14 '11 at 22:59
  • 1
    @user303352 actually you don't have an INSTANCE of EmpDAO UNLESS you create one or let spring create it and add it to EmpManager: Using @Autowired or . The Compiler don't give a sh*t about when you want to create your instances, it just knows that EmpManager has a reference to EmpDao called empDao – Mauricio Feb 14 '11 at 23:01
  • you are right, sorry. ok sorry for wasting your times guys, thanks for your help and effort make it clear. – cscsaba Feb 14 '11 at 23:06
  • Thanks Bozho, the main problem was my lack of knowledge, because private EmpDao empDao is not an existing object - as I thought - it is just a reference. – cscsaba Mar 01 '11 at 13:56
1

When the server bootstraps itself. It finds

 <context:annotation-config />

in the application context and then goes through the classes defined in the contexts. If there are any beans that are autowired, it injects that into the class by referring the context file.

Basically, it promotes convention over configuration. That's what most frameworks do these days to reduce the development time.

  • I have a same answer like for Bozho: If the Spring makes instance of EmpManager than it will make an EmpDao empDao as well in the same time. With or without @Autowired we have empDao as well – cscsaba Feb 14 '11 at 22:40
  • It instantiates all the classes in the context file and because of the , it goes through all the instantiated classes to inject the dependencies. So its like create first, scan and then inject. – Vanchinathan Chandrasekaran Feb 14 '11 at 22:43
  • Maybe I dont know what is the injection in real because as for me when Spring goes through and create e.g EmpManager than it makes and empDao as well. empDao automatically created during the creation of EmpManager. – cscsaba Feb 14 '11 at 22:48
  • Ok, I will check what is the convention over configuration or configuration over the convention. ? – cscsaba Feb 14 '11 at 23:03
  • ok sorry for wasting your times guys, thanks for your help and effort make it clear. – cscsaba Feb 14 '11 at 23:09
1

the @Autowired Spring annotation tells Spring to for a bean named 'empDao' and inject it into the EmpManager class, without you having to add the empDao bean as a property in your spring config file.

elduff
  • 1,178
  • 3
  • 14
  • 22
  • I have a same answer like for Bozho:But if the Spring makes instance of EmpManager than it will make an EmpDao empDao as well in the same time. With or without @Autowired we have empDao as well – cscsaba Feb 14 '11 at 22:37
  • ok sorry for wasting your times guys, thanks for your help and effort make it clear. – cscsaba Feb 14 '11 at 23:09
0

@Autowired tells Spring to find a bean of the declared type and wire in that bean, rather than requiring an explicit lookup by bean name. It can, under certain circumstances, make configuring applications easier if you only have one implementation of your types in a given Spring context.

James Kingsbery
  • 7,298
  • 2
  • 38
  • 67
  • I saw the differences, but everything can be figured out from the declaration without using the @Autowired. see Bozho's comment he point at my misbelief. Declaration is interpreted by compiler, annotation informs the Spring about the relation. – cscsaba Feb 14 '11 at 22:26
  • ok sorry for wasting your times guys, thanks for your help and effort make it clear. – cscsaba Feb 14 '11 at 23:10