AutoWired Setters rather than AutoWired instance variables in Spring

Donovan Thomson

I am a junior engineer on a Java team that uses Spring. We have been coached to Autowired Setters of instance variables rather than Autowiring the actual instance variable. I am slightly confused as to why we do this, what are the advantages of doing this and possible shortfalls of handling dependency management in this way ?

Marek Raki

Some observations from me (actually 3 years in Java EE):

Advantages:

  • you can add some extra validation or logic in setters methods
  • you can avoid to use reflection in junit testing for some special cases
  • setters can be overridden so you can inject something else

Disadvantages:

  • in most cases this validation is not necessary because you are injecting another services
  • you have to have a lot of useless setters and getters in your code. For example in my code I am using normally about 5 DAO classes per service so this is 10 extra methods to write which gives me about 60 extra lines.
  • mostly you do not need to inject another DAO or Service after the application is initialized.
  • setters/ getters can not be used later because in spring you are working on interfaces rather than concrete implementations. It is a bad practice to have setters and getters on interfaces.

Personally I prefer instance variable or constructor injections. But this is service dependent. I would not say that using setters is better. It has just different purposes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring @Autowired on a class new instance

From Dev

Is there way to reload autowired instance or replace autowired behavior in spring

From Dev

JSF + Spring Using @Autowired in a ManagedBean ; Do we need setters and getters

From Dev

multiple instance of autowired bean

From Dev

Spring boot @Autowired repository instance null when using spring security

From Dev

How Spring @Autowired binds the SessionFactory object even if there is no SessionFactory instance available

From Dev

How to use @Autowired in manually new instance in Spring Boot

From Dev

why @autowired in spring does not need setter method for private instance variable?

From Dev

Spring Couldn't autowired,there is more than one bean of `` type

From Dev

Spring Couldn't autowired,there is more than one bean of `` type

From Dev

Spring Inheritance Autowired Annotations

From Dev

Spring: @Autowired not working with ApplicationContext

From Dev

@Autowired & Spring context hierarchy

From Dev

Spring 3 Autowired BeanInitializationException

From Dev

Spring Autowired null if not found

From Java

Understanding Spring @Autowired usage

From Dev

Spring pure annotation with autowired

From Dev

Singelton in Spring with Autowired field

From Dev

Spring @Autowired Failed

From Dev

Spring 4 @AutoWired Failed

From Dev

@Autowired in Spring PermissionEvaluator

From Dev

Spring bean created but not autowired

From Dev

Spring Boot Autowired null

From Dev

Spring prototype scope with autowired

From Dev

Spring @Autowired comes as null

From Dev

Spring @Autowired and Singletons

From Dev

Using @Autowired in Spring 3

From Dev

Specify order to Spring @Autowired

From Dev

How is @Autowired implemented in Spring

Related Related

HotTag

Archive