2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Important features of autowiring in Spring?

Currently I am learning Spring IOC or Dependency Injection and autowiring concept make me cry to understand and I stuck there only. Can any one explain me the important features of this and how its work?

e.g I have a service class called UserServiceImpl that implements UserService interface then how would this be @Autowired?

Please suggest!

userimage
Some important and basic features of @Autowired :
  • Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

  • Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.

  • Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

  • Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.

  • In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.

  • In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names.

    Example to use @Autowired in your application :


    The @Autowired annotation tells Spring where an injection needs to occur. If you put it on a method setMovieFinder it understands (by the prefix set + the @Autowired annotation) that a bean needs to be injected. In the second scan, Spring searches for a bean of type MovieFinder, and if it finds such bean, it injects it to this method. If it finds two such beans you will get an Exception. To avoid the Exception, you can use the @Qualifier annotation and tell it which of the two beans to inject in the following manner:

    @Qualifier("redBean")
    class Red implements Color {
       // Class code here
    }

    @Qualifier("blueBean")
    class Blue implements Color {
       // Class code here
    }


    Or if you prefer to declare the beans in your XML, it would look something like this:

    <bean id="redBean" class="com.mycompany.movies.Red"/>
    <bean id="blueBean" class="com.mycompany.movies.Blue"/>


    In the @Autowired declaration, you need to also add the @Qualifier to tell which of the two color beans to inject:

    @Autowired
    @Qualifier("redBean")
    public void setColor(Color color) {
      this.color = color;
    }

    If you don't want to use two annotations (the @Autowired and @Qualifier) you can use @Resource to combine these two:

    @Resource(name="redBean")
    public void setColor(Color color) {
      this.color = color;
    }


    Answer is