Circular dependencies (a) in the Spring

Spring embodiment where the single circular dependencies are supported by default

First, what is the Dependency Injection

The so-called dependency, give an example, a Person class, another class Car, for example, if a method Person of the drive, need to refer Car, called the Person class depends on the Car class, extending to the object, this dependency is still valid , for example, the object of the Person class boy depend on the object TOYOTA Car class.

Public Person{
...
public void drive(){
  Car toyota=new Car("TOYOTA");
  toyota.挂档;
  toyota.踩油门;
  toyota.打方向;
}
}

This is one of dependency, has led to the need to be responsible for creating objects boy TOYOTA objects, and even manage the entire life cycle, and this will obviously bring a high degree of coupling, easy maintenance shortcomings, for example, to let the boy driving a car audi, you also need to modify the code of the class Person.

Therefore, in the design of java theory put forward a very well-known principles, Dependency Inversion principle (Dependence Inversion), the core idea is that you want this particular dependencies between classes, try to convert the abstract dependent, that is to say class Person it should depend on abstract classes ICar, rather than specific classes Car, java here to strongly recommend the use of abstraction and interfaces

Inversion of Control is another way of saying that is dependency injection (dependence injection), the above text boy with TOYOTA for example, the core is dependent on the object you want boy TOYOTA injected into the boy go, without reference to the boy himself TOYOTA, this injection process is usually controlled by a program to complete, without having to care about the object, for example as follows:

Public Person{
private ICar car;
public Person(ICar onecar){
  car=onecar;
}
public void drive(){
  car.挂档;
  car.踩油门;
  car.打方向;
}
}

Process this time are implanted and calls, very simple, as follows:

Toyota toyota=new Toyota();
Person boy=new Person(toyota);
boy.drive();

Here we assume, Toyota ICar class is a concrete implementation of the interface class. This example will demonstrate an example of a simple embodiment of the injection, i.e. injecting constructor manner to achieve the object constructor injected by the dependent objects.

There is also a commonly used injection method, the injection mode is the property, meaning implanted into the object by object properties will depend on the achieved or boy of example and Toyota described as follows:

Public Person{
private ICar car;
public Person(){
}
public void drive(){
  car.挂档;
  car.踩油门;
  car.打方向;
}
public ICar getCar(){
  return this.car;
}
public void setCar(ICar onecar){
  car=onecar;
}
}
Toyota toyota=new Toyota();
Person boy=new Person();
boy.setCar(toyota);
boy.drive();

Well, Spring Bean's life cycle steps Where do complete dependency injection

发布了25 篇原创文章 · 获赞 22 · 访问量 3635

Guess you like

Origin blog.csdn.net/weixin_42443419/article/details/104131848
Recommended