Question

I'm curious to know what's the difference between code like this:

class MyClass {
   @Autowired
   MyService myService;
}

and code like this:

class MyClass {
   MyService myService;

   @Required
   public void setMyService(MyService val) {
       this.myService = val;
   }
}
Was it helpful?

Solution

@Autowired annotation is used when you want to autowire a bean. @Autowired is not limited to setter. It can be used with a constructor and a field as well. If you use @Autowired annotation on a field, that field will be autowired with bean having matching data type.

@Required checks if a particular property has been set or not. If a field has been annotated with @Required annotation and that field is not set, you will get org.springframework.beans.factory.BeanInitializationException.

Refer:

Spring @Autowired usage

Recommended usage of Spring's @Required annotation

Edit: As pointed by 'kryger': field annotated with @Autowired is effectively also @Required (unless you explicitly set its parameter required to false). eg:

@Autowired(required=false)
private ObjectType objectType;

For a field that has been annotated @Autowired, if bean with matching data type in not available, org.springframework.beans.factory.BeanCreationException is thrown.

OTHER TIPS

@Autowired is not the same as @Required.

The @Autowire-Annotation (as in your code-example), tells the ApplicationContext (a.k.a the Spring-IoC-Containter) to inject the desired dependency. (No matter how, if its by using annotations or the XML-File of the ApplicationContext).

The @Required-Annotation, tells the ApplicationContext that this property has to be mentioned in the XML-file (The XML-File of the ApplicationContext), which than leds to the dependency being injected by using the XML-File (or to an expection of course). But the Annotation on its own doesn't tell to inject the dependency! The injection is done because the property is mentioned in the XML-file. That's good to know, you may need it.

With mentioning the property in a XML-File I mean such a configuration for instance:

<bean id="MyClass" class="com.myclasses.common.MyClass">
     <property name="someProperty" value="ValueThatHasToBeInjected" />
</bean>

So why should I use it over the @Autowired-Annotation?

You should use it when the dependency has to be injected by the information in the XML-configuration file.

Can you give me an example?

Well, there is already a very good example on this website. where this is also explained.

@watchme @Required -annotation tells to the container that this property needs to be initialized as part of configuration, it can be initialized either through xml configuration or through @Autowired annotation or through java bean creation.

  1. xml example:

    public class SimpleMovieLister 
    { 
     private MovieFinder movieFinder;
    
     @Required
     public void setMovieFinder(MovieFinder movieFinder)
     {
        this.movieFinder = movieFinder;
     }
    }
    
    <beans ...>
    <bean id = "movieFinder" class = "com.test.MovieFinder">
    
    <bean id = "simpleMovieLister" class = "com.test.SimpleMovieLister">
     <property name = "movieFinder" value = "movieFinder" />
    
  1. through @Autowire

    public class SimpleMovieLister 
    { 
      @Autowired 
      @Required
      private MovieFinder movieFinder;
    }
    
    

Here truly speaking we don't need @Required as it @Autowired default is Required.

  1. thru @Bean

    public class SimpleMovieLister 
    { 
      private MovieFinder movieFinder;
    
      @Required
      public void setMovieFinder(MovieFinder movieFinder)
      {
         this.movieFinder = movieFinder;
      }
    }
    
    
    public class AppConfig {
    
    @Bean
    public SimpleMovieLister getSimpleMovieLister()
    {
       SimpleMovieLister simpleMovieLister = new SimpleMovieLister();
       simpleMovieLister.setMovieFinder(new MoveFinder());
    }
    }
    
    
    
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top