IOC 容器入门案例 {#IOC 容器入门案例}
- 创建 maven 项目, 导入坐标
<?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>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.mobaijun<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring_mvc<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token comment"><!-- spring 的 IOC 坐标 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-context<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.0.2.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span>
<span class="token comment"><!-- 导入 junit 测试 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>4.12<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span>
</project>
- 创建 User 对象
package com.mobaijun;
/**
`
`
*
@Author: Auser·杰
*
Development: IntelliJ IDEA 2018.2.4 x64
*
ClassName: User
*
ClassCreateDate: 2020/1/3 14:15
* `
`类简介:
*/
public class User {
`
`
public` `User()` `{`
`System.`out`.println("创建了 User 对象!");`
`}`
`}
-
resource
目录下新建 application.xml 文件 -
编写 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">
<!-- 创建 User 对象 -->
<!--
id: 对象的别名,用于获取对象
class: 需要创建的对象,必须是类的全限定名
-->
<bean id="user" class="com.mobaijun.User"/>
<bean id="user2" class="com.mobaijun.User2"/>
<bean id="user3" class="com.mobaijun.User3"/>
</beans>
- test 目录下新建 Demo 测试类
package com.mobaijun;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
`
`
*
@Author: Auser·杰
*
Development: IntelliJ IDEA 2018.2.4 x64
*
ClassName: Demo1
*
ClassCreateDate: 2020/1/3 14:17
* `
`类简介:
*/
public class Demo1 {
`
`
@Test
public void test1() {
// 创建 IOC 容器 (模拟 beanFactory)
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
User user = (User) ac.getBean("user");
System.out.println("user =" + user);
}
`
`
@Test
public void test2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
User2 user2 = (User2) ac.getBean("user2");
System.out.println("user2 =" + user2);
}
`
`
@Test`
`public` `void` `test3()` `{`
`ApplicationContext` ac `=` `new` `ClassPathXmlApplicationContext("bean.xml");`
`User3` user3 `=` `(User3)` ac`.getBean("user3");`
`System.`out`.println("user3 ="` `+` user3`);`
`}`
`}
小结:
创建项目,导入 spring-context 坐标
2)创建 User 对象
3)创建 bean.xml,定义
4)创建 IOC 容器(ApplicationContext),从容器获取对象 getBean("id")