0

What are the differences between the following types of Constructor injection in Spring?

@Component public class MyService { private final MyRepository myRepository; public MyService(@Autowired MyRepository myRepository) { this.myRepository = myRepository; } //rest of code } 

and

@Component public class MyService { private final MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } //rest of code } 

Also - which one is better practice?

2 Answers 2

1

You can use Constructor injections as below this is considered as the better practice

@Component public class MyService { private final MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } //rest of code } 

but there is no difference in both forms of constructor injection. and one thing more you don't have to declare the object in constructor injection as you can do it like this in spring boot:

 @Component public class MyService { @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } //rest of code } 

or don't use autowired because the constructor will auto wire automatically like this-

@Component public class MyService { private final MyRepository myRepository; //autowire by constructor public MyService(MyRepository myRepository) { this.myRepository = myRepository; } //rest of code } 

In the below form it does not require Autowired, it will work though, but it doesn't mean anything. The best practice is you use constructor injection as above forms.

@Component public class MyService { private final MyRepository myRepository; public MyService(@Autowired MyRepository myRepository) { this.myRepository = myRepository; } //rest of code } 
Sign up to request clarification or add additional context in comments.

Comments

0

When you are doing constructor injection . You do not even have to write @Autowired. Spring automatically looks for bean and inject it . If you remove the autowired from above . It will still work

5 Comments

However, if you have more than one beans extended to one interface. You can define which one to inject using @Qualifier annotation to specify which one would you like to inject. However, if you only have one bean with that class name. The spring will inject the bean automatically
OK, in the above example 1 what is the significance of having @autowired on the constructor parameter? I have not seen that before
It is same in both the declarations. It tells spring to inject the dependency of that class name. However, it doesn't have any specific significance you can use both ways and you can remove the autowired annotation too. It is just different ways to inject . There is no specific significance for this.
Also, the second way is recommended if you are using autowired annotation . As if you are using the first way . You will have to write autowired with every field if you are injecting more than one bean through a constructor.
However, Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the @Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies. Regarding the default constructor: You either need the default constructor, a constructor with the @Autowired annotation when you have multiple constructors, or only one constructor in your class with or without the @Autowired annotation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.