How does autowiring work in Spring?

后端 未结 11 1572
执笔经年
执笔经年 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:11

    Keep in mind that you must enable the @Autowired annotation by adding element into the spring configuration file. This will register the AutowiredAnnotationBeanPostProcessor which takes care the processing of annotation.

    And then you can autowire your service by using the field injection method.

    public class YourController{
    
     @Autowired
     private UserService userService; 
    
    }
    

    I found this from the post Spring @autowired annotation

提交回复
热议问题