Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use autowiring in Spring

Tags:

java

spring

I am reading the book Pro Spring 3. It has a certain paragraph that really confused me. The paragraph is about autowiring in spring. Here is an excerpt:

In most cases, the answer to the question of whether you should use autowiring is definitely “no!” Autowiring can save you time in small applications, but in many cases, it leads to bad practices and is inflexible in large applications. Using byName seems like a good idea, but it may lead you to give your classes artificial property names so that you can take advantage of the autowiring functionality. The whole idea behind Spring is that you can create your classes how you like and have Spring work for you, not the other way around ...

... For any nontrivial application, steer clear of autowiring at all costs.

I have always been using the @Autowired tag in applications I have created. Can someone explain what is wrong with it and what I should use instead?

A mini example on how I handle most things now is:

@Service("snippetService") public class SnippetService {      @Autowired     private TestService testService;      public Snippet getSnippet() {         return testService.getSnippet();     } }     

Is using autowiring like this "wrong" or am I missing something?

like image 655
Geoffrey De Vylder Avatar asked Oct 16 '12 17:10

Geoffrey De Vylder


People also ask

Why do we use Autowiring?

Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. Autowiring can't be used to inject primitive and string values. It works with reference only.

What is the difference between @bean and @autowired?

@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).

Is Autowired required in Spring boot?

No. After Spring 4.3 If your class has only single constructor then there is no need to put @Autowired .

Should Autowired be used?

In most cases, the answer to the question of whether you should use autowiring is definitely “no!” Autowiring can save you time in small applications, but in many cases, it leads to bad practices and is inflexible in large applications.


2 Answers

I believe there are two things confused here. What is meant by 'autowiring' in this chapter is marking bean for automated detection and injection of dependencies. This can be achieved through setting of "autowire" bean attribute.

This is in fact opposed to using @Autowired where you explicitely indicate field or setter for dependency injection.

Have a look here: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-autowire.

To explain it, assume you have

public class SnippetService {      private TestService testService;      public Snippet getSnippet() {         return testService.getSnippet();     }      public void setTestService(TestService testService) {       this.testService = testService;     } } 

If you defined a bean:

<bean class="mypackage.SnippetService" autowire="byType"/> 

spring would attempt to inject bean of matching type, TestService in this case, by calling setTestService setter. Even though you did not use @Autowired. This indeed is dangerous since some setters might not be meant to be called by spring.

If you set autowire="no", nothing will be injected unless marked so with @Autowired, @Resource, @Inject.

like image 157
mrembisz Avatar answered Oct 03 '22 19:10

mrembisz


There's nothing wrong with what you have, especially if you are starting out with one implementation of TestService anyways. As Johan mentions though, it's better to use @javax.annotation.Resource which also allows you to be more specific if you need to (for example using the name or the type attribute).

like image 43
Abdullah Jibaly Avatar answered Oct 03 '22 19:10

Abdullah Jibaly