├── iocinit.iml ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── terwergreen │ │ │ └── spring │ │ │ └── iocinit │ │ │ ├── IOCTest1.java │ │ │ ├── IOCTest2.java │ │ │ ├── IOCTest3.java │ │ │ └── beans │ │ │ ├── America.java │ │ │ ├── Chinese.java │ │ │ └── Person.java │ │ └── resources │ │ └── applicationContext.xml │ └── test │ └── java
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.terwergreen.spring</groupId> <artifactId>iocinit</artifactId> <version>1.0.0</version> <dependencies> <!-- spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.6.RELEASE</version> </dependency> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies> </project>
先創建幾個beanjava
America.java
spring
package com.terwergreen.spring.iocinit.beans; public class America implements Person { public String sayHello(String name) { return "hello," + name; } }
Chinese.java
apache
package com.terwergreen.spring.iocinit.beans; /** * Chinese * * @author Terwer * @version 1.0 * 2019/5/6 17:39 **/ public class Chinese implements Person { public String sayHello(String name) { return "你好," + name; } }
Person.java
app
package com.terwergreen.spring.iocinit.beans; /** * Person * * @author Terwer * @version 1.0 * 2019/5/6 17:39 **/ public interface Person { String sayHello(String name); }
XmlBeanFactory
實現IOCpackage com.terwergreen.spring.iocinit; import com.terwergreen.spring.iocinit.beans.Person; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; /** * IOCTest1 * * @author terwer * @version 1.0 * 2019-05-17 21:38 **/ public class IOCTest1 { public static void main(String[] args) { ClassPathResource resource = new ClassPathResource("applicationContext.xml"); XmlBeanFactory beanFactory = new XmlBeanFactory(resource); Person p1 = (Person) beanFactory.getBean("chinese"); String result = p1.sayHello("Terwer"); System.out.println(result); Person p2 = (Person) beanFactory.getBean("america"); String result2 = p2.sayHello("Green"); System.out.println(result2); } }
輸出maven
你好,Terwer hello,Green
DefaultListableBeanFactory
實現IOCpackage com.terwergreen.spring.iocinit; import com.terwergreen.spring.iocinit.beans.Person; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ClassPathResource; /** * IOCTest2 * * @author terwer * @version 1.0 * 2019-05-17 21:45 **/ public class IOCTest2 { public static void main(String[] args) { // 定位 ClassPathResource resource = new ClassPathResource("applicationContext.xml"); // 載入 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); // 註冊 reader.loadBeanDefinitions(resource); // 從beanFactory獲取bean Person p1 = (Person) beanFactory.getBean("chinese"); String result = p1.sayHello("Green"); System.out.println(result); } }
輸出spa
你好,Green
ClassPathXmlApplicationContext
實現IOCpackage com.terwergreen.spring.iocinit; import com.terwergreen.spring.iocinit.beans.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * IOCTest3 * * @author terwer * @version 1.0 * 2019-05-17 21:47 **/ public class IOCTest3 { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person p1 = (Person) applicationContext.getBean("america"); String result = p1.sayHello("Test3 from applicationContext"); System.out.println(result); } }
輸出code
hello,Test3 from applicationContext