Posts

Showing posts with the label @Autowired

Spring @Autowired Annotation Example

Image
In this section we will learn about @Autowired  Annotation.   We can use the  @Autowired  to mark a dependency which Spring is going to resolve and inject. If  @Autowired  is applied to a field: then the dependency is stored in this  field. a setter: then the setter is invoked, with the parameter that is determined by the same algorithm like for the field dependency  injection. a constructor: then the constructor is invoked with the parameters determined by the same algorithm like for the field dependency injection.     Constructor Injection @RestController public class UserController { private UserService userService; @Autowired public UserController ( UserService userService ) { this . userService = userService; } } Setter Injection @RestController public class UserController { private UserService userService; @Autowired public void setUserService ( UserService userService ) { this . userService = userService; }