Preface
Inversion of control (IoC) is used to solve coupling, and is mainly divided into two types: dependency injection and dependency lookup.
Dependency injection is to inject the dependencies that should be in the program externally into the program. Of course, it is also a concept of design patterns.
Assuming that there is implementation B of interfaces A and A, this code will be executed A a=new B(); at this time, a certain dependency will inevitably occur. However, the interface appears to solve the dependency, but coupling will still occur in doing so. We can use dependency injection to achieve decoupling. In Ioc, you can put the code you want to depend on into XML, and form this dependency relationship when needed through a container, that is, inject the required interface implementation into the class that needs it. This may be the source of the term "dependency injection".
Simple understanding of dependency injection
So, let’s put aside Spring and XML related technologies now, how can we use the easiest way to implement a dependency injection? Now it is still the implementation of interfaces A and A.
So our purpose is like this, A a=new B(); Now we are defining a class C, and the following is the relationship between C and A. In order to new, C creates an implementation class for A interface
public class C { private A a; public C(A a) { this.a=a; }}So how to use new? Define a class D, and call the constructor of C in D new B(); that is
public class D{ @Test public void Use(){ C c=new C(new B()); }}In this way, we will not have dependencies between A and B in C. If we want to change the implementation class of A, we can directly modify the parameters of the construction method in D. It is very simple and also solves the coupling. This method is the most commonly known as constructor injection.
Then Spring is the solution to coupling and using Ioc. Here is the simplest example of Spring dependency injection:
SpringConfig.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sayhello"> <constructor-arg ref="hello"/> </bean> <bean id="hello" /></beans>
Analysis: Two beans are configured here. The first is to inject a bean into the constructor, and the second is the bean to be injected into the constructor.
Hello.java
package smile;/** * Created by smile on 2016/4/21. */public class Hello { public Hello(){ System.out.println("Hello"); } public void saysHello(){ System.out.println("I want to say Hello"); }}TheInterface.java
package smile;/** * Created by smile on 2016/4/20. */public class TheTestInterface { private Hello hello; public TheTestInterface(Hello hello) { this.hello = hello; }}Use.java
package com.smile;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import smile.Hello;/** * Created by smile on 2016/4/21. */public class Use { @Test public void UseTest(){ ApplicationContext context=new ClassPathXmlApplicationContext("SpringConfig.xml"); Hello hello=(Hello)context.getBean("hello"); hello.sayHello(); }}Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.