Think about the Java you learned before. If a class needs to refer to an object, it needs to manually create a new one. The problem with this is that if the referenced class is changed or deleted, all the classes that reference it will report an error. Because the two classes are coupled together. The solution is to be controlled by a third-party configuration file instead of the active reference of the class itself. This is the so-called inversion of control. Here is an example.
The environment needs to be prepared before the presentation.
IDE: Eclipse Java EE Mars
Spring's jar package: The official website does not provide downloads, but recommends building projects using Maven or Gradle. However, in order to facilitate the demonstration, the original form is still used, and the following address can be downloaded.
http://repo.springsource.org/libs-release-local/org/springframework/
When you're ready, create a new Java project and import the jar package you need. To implement this example, the following seven jar packages are required, the last four can be found in the downloaded Spring package, and the first three need to be downloaded separately.
After importing the jar package, Add Library and select JUnit. Then build the following directory.
HelloApi.java:
package com.erving; public interface HelloApi { public void sayHello(); }
HelloImpl.java:
package com.erving; public class HelloImpl implements HelloApi { @Override public void sayHello() { System.out.println("Hello World!"); } }
After the interface and implementation of business logic are written, the configuration file is defined to enable it to be managed by the IOC container. The configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="hello" class="com.erving.HelloImpl"></bean> </beans>
Finally, how to call this function? There are only three steps: one is to instantiate the IOC container; the other is to get beans from the container; and the third is to call the interface to complete the function.
HelloTest.java:
package com.erving; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloTest { @Test public void testHelloWorld() { ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml"); HelloApi helloApi = context.getBean("hello", HelloApi.class); helloApi.sayHello(); } }
In particular, it should be noted that the class specified in the configuration file is the Implement of the function, and the Interface of the function is used to get the Bean from the IOC container.