51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

【Java 基础】IOC 容器入门案例

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">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.mobaijun<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring_mvc<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!-- spring 的 IOC 坐标 --&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-context<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>5.0.2.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!-- 导入 junit 测试 --&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>4.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</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")

赞(0)
未经允许不得转载:工具盒子 » 【Java 基础】IOC 容器入门案例