Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring @Autowire property vs setter

What is the difference between anotate @Autowired to a property or do it in the setter?

As far as I know they both have the same result, but is there any reason to use one over the other?

UPDATE (to be more concise)

Is there a difference between this

package com.tutorialspoint;  import org.springframework.beans.factory.annotation.Autowired;  public class TextEditor {    private SpellChecker spellChecker;     @Autowired    public void setSpellChecker( SpellChecker spellChecker ){       this.spellChecker = spellChecker;    }     public void spellCheck() {       spellChecker.checkSpelling();    } } 

and this

package com.tutorialspoint;  import org.springframework.beans.factory.annotation.Autowired;  public class TextEditor {    @Autowired    private SpellChecker spellChecker;     public TextEditor() {       System.out.println("Inside TextEditor constructor." );    }     public void spellCheck(){       spellChecker.checkSpelling();    } } 
like image 995
luso Avatar asked Nov 06 '15 08:11

luso


People also ask

Is @autowired setter injection?

@Autowired on Setter MethodsYou can use @Autowired annotation on setter methods to get rid of the <property> element in XML configuration file. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.

Which is better setter or constructor injection in Spring?

Setter Injection has upper hand over Constructor Injection in terms of readability. Since for configuring Spring we use XML files, readability is a much bigger concern.

What is difference between @autowired and @resource in Spring?

The main difference is is that @Autowired is a spring annotation whereas @Resource is specified by the JSR-250. So the latter is part of normal java where as @Autowired is only available by spring.

Why is constructor injection better than Autowired?

Constructor injection makes code more robust. It allows us to create immutable objects, preventing NullPointerException s and other errors. You can find the code example on GitHub.


2 Answers

Sometimes you need an instance of class A, but you do not store A in the fields of the class.
You just need A instance to perform a one-shot operation. Or, you use A instance to obtain an instance of B, and you are storing B in the field.

In those cases, a setter (or constructor) autowire will suit you better.
You will not have unused class-level fields.

Concrete example:
You need to construct RabbitTemplate (an object that sends messages to RabbitMQ) To construct it, you need ConnectionFactory
http://docs.spring.io/spring-amqp/docs/latest_ga/api/org/springframework/amqp/rabbit/core/RabbitTemplate.html#RabbitTemplate-org.springframework.amqp.rabbit.connection.ConnectionFactory-

You do not need to store that ConnectionFactory. In that case, code that looks like this:

Class MyClass { private RabbitTemplate template;  @Autowired  void setConnectionFactory(ConnectionFactory c) {     template=new RabbitTemplate(c); } } 

...will serve you better than directly autowiring the ConnectionFactory field.

In this example, autowiring at the constructor level would be even better, because your object will always be completely constructed. It will be clear that ConnectionFactory is a mandatory dependency, not an optional one.

like image 169
Bartosz Bilicki Avatar answered Sep 22 '22 08:09

Bartosz Bilicki


With @Autowired annotation, you don't need a setter method. Once your bean's constructor is done with allocating/creating the object, Spring will scan for this annotation and would inject the object instances that you annotated.

While if you have setter and if you are still using xml config, you would explicitly set properties.

Having said that, You could annotate your constructor and setter method with autowired annotation which i would prefer as this would give me flexibility later on to move away from Spring (although i wont do it).

like image 26
SMA Avatar answered Sep 20 '22 08:09

SMA