The previous blog probably spoke about the understanding of the IOC container, so the implementation of the IOC actually relies on dependency injection. Simply put, the IOC is an idea, and dependency injection provides the realization of this idea. This is what the individual understands. This blog describes two commonly used injection methods, as well as their configuration (XML-based).
IOC Container Descriptionfrom recent learning, especially in the form of XML-based configuration. An IOC container is a production line that, depending on the holding relationship between classes in a configuration, assembles a component into a complete product to perform a complete task. From the user's direction, he can only see an entrance, and after the treatment he is not aware of. Of course, this is the same when there is no concept of a container. The difference is that the container is an assembler and only it knows the relationships between the parts, and it is responsible for combining the pieces into a complete product. That's probably what it means:
Dependency InjectionAs I said before, inversion of control is reversed, and the function of the created callee is reversed and given to the IOC container for processing. What is dependency injection? It is the way that the IOC provides the caller with the caller. The IOC container injects the callee into the caller, just like an injection, without the caller creating the user himself at runtime. There are two types of injection methods that are most commonly used: Setter method injection and constructor injection.
Setter Methodthat is, the set method injects the Bean's attribute value or dependent object. This method of injection requires the provision of a default parameterless constructor. If there is no constructor, the JVM provides a default parameterless constructor by default. When you write a constructor, you need to write a constructor without parameters because the JVM no longer provides a default constructor. Otherwise throws an exception.
Car Class
public class Car {private string company;private string brand;private int maxspeed;private float price; public void Setbrand (String brand) { this.brand = brand;} public void setmaxspeed (int maxspeed) { this.maxspeed = maxspeed;} public void Setprice (double price) { this.price = Price;} public void Setcompany (String company) { this.company = Company;}}
Configuration
<bean id = "Car" class= "Com.tgb.spring.car" > <property name = "Brand" ><value> Mini </value> </property><property name = "Company" ><value> BMW </value></property><property name = "Price" ><value>220000</value></property><property name = "Maxspeed" ><value>200 </value></property></bean>
This approach is flexible and easy to configure, as can be seen above. It is also important to mention here that spring only checks for the existence of a corresponding set method in the bean and does not check for the existence of a corresponding variable when injected through the set method. Just, good habits, usually define variables.
constructor FunctionSetting the value of the property and the dependent object in the way of the constructor is a more common way than the set method. Here, you need to provide a constructor with parameters.
Car Class
public class Car {private string company;private string brand;private int maxspeed;private float price;public Car (string c Ompany, String brand, float price) {super (); this.company = Company;this.brand = Brand;this.price = Price;} Public Car (String company, string brand, int maxspeed) {super (); this.company = Company;this.brand = Brand;this.maxspeed = Maxspeed;} Public Car (String company, String brand, Int. Maxspeed, float price) {super (); this.company = Company;this.brand = Brand;thi S.maxspeed = Maxspeed;this.price = Price;}}
Configuration
<bean id= "Car" class= "Com.atguigu.spring.helloworld.Car" ><constructor-arg value= "KUGA" index= "1" ></ Constructor-arg><constructor-arg value= "Changanford" index= "0" ></constructor-arg>< Constructor-arg value= "250000" type= "float" ></constructor-arg></bean>
First notice here that there are three constructors with parameters in the bean, and another thing to note is that the index and type two tags are introduced in the configuration. Here, you can pinpoint which constructor you want to use by the number of parameters, the order of parameters, and the type of parameter you configure. This one's own experience.
otherThe configuration features provided by spring are rich, and there are some interesting configurations, such as internal bean configuration, auto-assembly, and so on, that deal with basic configuration.
Spring Learning Summary (1.2)-Dependency Injection and configuration understanding