How does autowiring work in Spring?

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

    Spring dependency inject help you to remove coupling from your classes. Instead of creating object like this:

    UserService userService = new UserServiceImpl();
    

    You will be using this after introducing DI:

    @Autowired
    private UserService userService;
    

    For achieving this you need to create a bean of your service in your ServiceConfiguration file. After that you need to import that ServiceConfiguration class to your WebApplicationConfiguration class so that you can autowire that bean into your Controller like this:

    public class AccController {
    
        @Autowired
        private UserService userService;
    } 
    

    You can find a java configuration based POC here example.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 16:10

    @Autowired is an annotation introduced in Spring 2.5, and it's used only for injection.

    For example:

    class A {
    
        private int id;
    
        // With setter and getter method
    }
    
    class B {
    
        private String name;
    
        @Autowired // Here we are injecting instance of Class A into class B so that you can use 'a' for accessing A's instance variables and methods.
        A a;
    
        // With setter and getter method
    
        public void showDetail() {
            System.out.println("Value of id form A class" + a.getId(););
        }
    }
    
    0 讨论(0)
  • 2020-11-22 16:11

    Keep in mind that you must enable the @Autowired annotation by adding element <context:annotation-config/> 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

    0 讨论(0)
  • 2020-11-22 16:14

    The whole concept of inversion of control means you are free from a chore to instantiate objects manually and provide all necessary dependencies. When you annotate class with appropriate annotation (e.g. @Service) Spring will automatically instantiate object for you. If you are not familiar with annotations you can also use XML file instead. However, it's not a bad idea to instantiate classes manually (with the new keyword) in unit tests when you don't want to load the whole spring context.

    0 讨论(0)
  • 2020-11-22 16:15

    How does @Autowired work internally?

    Example:

    class EnglishGreeting {
       private Greeting greeting;
       //setter and getter
    }
    
    class Greeting {
       private String message;
       //setter and getter
    }
    

    .xml file it will look alike if not using @Autowired:

    <bean id="englishGreeting" class="com.bean.EnglishGreeting">
       <property name="greeting" ref="greeting"/>
    </bean>
    
    <bean id="greeting" class="com.bean.Greeting">
       <property name="message" value="Hello World"/>
    </bean>
    

    If you are using @Autowired then:

    class EnglishGreeting {
       @Autowired //so automatically based on the name it will identify the bean and inject.
       private Greeting greeting;
       //setter and getter
    }
    

    .xml file it will look alike if not using @Autowired:

    <bean id="englishGreeting" class="com.bean.EnglishGreeting"></bean>
    
    <bean id="greeting" class="com.bean.Greeting">
       <property name="message" value="Hello World"/>
    </bean>
    

    If still have some doubt then go through below live demo

    How does @Autowired work internally ?

    0 讨论(0)
提交回复
热议问题