How does autowiring work in Spring?

后端 未结 11 1616
执笔经年
执笔经年 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: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(););
        }
    }
    

提交回复
热议问题