How does autowiring work in Spring?

后端 未结 11 1570
执笔经年
执笔经年 2020-11-22 15:35

I\'m a little confused as to how the inversion of control (IoC) works in Spring.

Say I have a service class called UserServic

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 16:07

    You just need to annotate your service class UserServiceImpl with annotation:

    @Service("userService")
    

    Spring container will take care of the life cycle of this class as it register as service.

    Then in your controller you can auto wire (instantiate) it and use its functionality:

    @Autowired
    UserService userService;
    

提交回复
热议问题