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 } Your firstIn 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 }