What is Dependency Injection and Inversion of Control in Spring Framework?

前端 未结 11 2325
你的背包
你的背包 2020-12-02 03:51

\"Dependency Injection\" and \"Inversion of Control\" are often mentioned as the primary advantages of using the Spring framework for developing Web frameworks

Could

相关标签:
11条回答
  • 2020-12-02 04:26

    IOC is technique where you let someone else to create the object for you. And the someone else in case of spring is IOC container.

    Dependency Injection is a technique where one object supplies the dependency of another object.

    0 讨论(0)
  • 2020-12-02 04:29

    In simple terms.. In simple terms..

    • IOC(Inversion of Control) is a concept means: Instead of creating objects by new operator,let the container do it for you.
    • DI(Dependency injection) is way to inject the dependency of a framework component by the following ways of spring:
    1. Contructor injection
    2. Setter/Getter injection
    0 讨论(0)
  • 2020-12-02 04:33

    IOC stands for inversion of control and is a higher level concept that states that we invert the control of the creation of objects from the caller to the callee.

    Without inversion of control, you are in charge of the creation of objects. In an inversion of control scenario a framework is in charge to create instances of a class.

    Dependency injection is the method through which we can achieve inversion of control. In order for us to leave the control up to the framework or job we declare dependencies and the IOC container injects those dependencies in our class (i.e. the framework creates an instance for us and provides that to our class).

    Now what are the advantages of this?

    First of all the classes and their lifecycle will be managed by Spring. Spring completely manages the process from creation to destruction.

    Secondly, you will get reduced coupling between classes. A class is not tightly coupled with an implementation of another class. If an implementation changes, or if you want to change the implementation of the injected interface you can do so easily without needing to change all the instances in your code base by hand.

    Third, there is an increased cohesion between classes. High cohesion means keeping classes that are associated with one another together. Because we are injecting interfaces in other classes it is clear which classes are necessary for the calling class to operate.

    Fourth, there is increased testability. Because we are using interfaces in the constructor we can easily swap out the implementation with a mock implementation

    fifth, the use of JDK dynamic proxy to proxy objects. the JDK dynamic proxy requires interfaces to be used which is true, because we are injecting these interfaces. This proxy can then be used for Spring AOP, transaction handling, Spring data, Spring security and more

    0 讨论(0)
  • 2020-12-02 04:34

    Inversion of control- It means giving the control of creating and instantiating the spring beans to the Spring IOC container and the only work the developer does is configuring the beans in the spring xml file.

    Dependency injection-

    Consider a class Employee

    class Employee { 
       private int id;
       private String name;
       private Address address;
    
       Employee() {
         id = 10;
         name="name";
         address = new Address();
       }
    
    
    }
    

    and consider class Address

    class Address {
       private String street;
       private String city;
    
       Address() {
         street="test";
         city="test1";
    
      }
    }
    

    In the above code the address class values will be set only when the Employee class is instantiated, which is dependency of Address class on Employee class. And spring solves this problem using Dependency Injection concept by providing two ways to inject this dependency.

    1. Setter injection

    Setter method in Employee class which takes a reference of Address class

    public void setAddress(Address addr) {
        this.address = addr;
    }
    
    1. Constructor injection

    Constructor in Employee class which accepts Address

    Employee(Address addr) {
          this.address = addr;
    }
    

    In this way the Address class values can be set independently using either setter/constructor injection.

    0 讨论(0)
  • 2020-12-02 04:35

    Spring: Spring is “Inversion of Control” container for the Java Platform.

    Inversion of Control (IoC): Inversion of Control (IoC) is an object-oriented programing practice whereby the object coupling is bounded at runtime by an "assembler" object and are typically not knowable at compile time using static analysis.

    Dependency Injection (DI): "Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time." -wiki.

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