• Ioc及Bean容器(三)


    专题一 IoC

    •  接口及面向接口编程
    •  什么是 IoC
    •  Spring 的Bean配置
    •  Bean 的初始化
    •  Spring 的常用注入方式

    接口

    •  用于沟通的中介物的抽象化
    •  实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式
    •  对应 Java 接口即声明了哪些方法是对外公开提供的
    •  在 Java8 中,接口可以拥有方法体

    面向接口编程

    •  结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类
    •  接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要
    •  “面向接口编程” 中的 “接口” 是用于隐藏具体实现和实现多态性的组件

    什么是IoC

    •  IoC :控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护
    •  DI (依赖注入)是其一种实现方式, IoC 的别名
    •  目的:创建对象并且组装对象之间的关系

    扩展理解

      2004年, Martin Fowler 探讨了同一个问题,既然 IoC 是控制反转,那么到底是“哪些方面的控制被反转了呢?”,经过详细地分析和论证后,他得出了答案:“获得依赖对象的过程被反转了”。控制被反转之后,获得依赖对象的过程由自身管理对象变为由 IoC 容器主动注入。于是,他给“控制反转”取了一个更合适的名字叫做“依赖注入(Dependency Injection,DI)”。他的这个答案,实际上给出了实现 IoC 的方法:注入。所谓依赖注入,就是由 IoC 容器在运行期间,动态地将某种依赖关系注入到对象之中。

    IoC房屋中介

      房屋中介         IoC

      1. 找中介     ----> 1. 找IoC容器

      2. 中介介绍房子  ----> 2. 找IoC容器

      3. 租房、入住   ----> 3. 使用对象

    自从有了IoC之后

    •  不必自己创建对象了
    •  IoC机制就提供了
    •  面向接口编程了
    •  IoC藏实现了
    •  不管对象了
    •  IoC管了
    •  变好了
    •  IoC
    •  爽

    单元测试

    •  下载 junit-*.jar 并引入工程
    •  创建 UnitTestBase 类,完成对 Spring 配置文件的加载、销毁
    •  所有的单元测试类都继承自 UnitTestBase ,通过它的 getBean 方法获取想要得到的对象
    •  子类(具体执行单元测试的类)加注解: @RunWith(BlockJUnit4ClassRunner.class)
    •  单元测试方法加注解: @Test
    •  右键选择要执行的单元测试方法执行或者执行一个类的全部单元测试方法

    UnitTestBase 类

    package com.imooc.test.base;
    
    import org.junit.After;
    import org.junit.Before;
    import org.springframework.beans.BeansException;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.util.StringUtils;
    
    public class UnitTestBase {
        
        private ClassPathXmlApplicationContext context;
        
        private String springXmlpath;
        
        public UnitTestBase() {}
        
        public UnitTestBase(String springXmlpath) {
            this.springXmlpath = springXmlpath;
        }
        
        @Before // Test 执行前执行
        public void before() {
            if (StringUtils.isEmpty(springXmlpath)) {
                springXmlpath = "classpath*:spring-*.xml";
            }
            try {
                context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\s]+"));
                context.start();
            } catch (BeansException e) {
                e.printStackTrace();
            }
        }
        
        @After // Test 执行结束后执行
        public void after() {
            context.destroy();
        }
        
        @SuppressWarnings("unchecked")
        protected <T extends Object> T getBean(String beanId) {
            try {
                return (T)context.getBean(beanId);
            } catch (BeansException e) {
                e.printStackTrace();
                return null;
            }
        }
        
        protected <T extends Object> T getBean(Class<T> clazz) {
            try {
                return context.getBean(clazz);
            } catch (BeansException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    配置 spring-ioc.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="oneInterface" class="com.imooc.ioc.interfaces.OneInterfaceImpl"></bean>
        
     </beans>

    测试类

    package com.imooc.test.ioc.interfaces;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import com.imooc.ioc.interfaces.OneInterface;
    import com.imooc.test.base.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestOneInterface extends UnitTestBase {
    
        public TestOneInterface() {
            super("classpath*:spring-ioc.xml");
        }
        
        @Test
        public void testSay() {
            OneInterface oneInterface = super.getBean("oneInterface");
            oneInterface.say("This is a test.");
        }
    
    }

    输出结果

    Bean容器初始化

    •  基础:两个包
        - org.springframework.beans
        - org.springframework.context
        - BeanFactory 提供配置结构和基本功能,加载并初始化 Bean
        - ApplicationContext 保存了 Bean 对象并在 Spring 中被广泛使用
    •  方式,ApplicationContext
        - 本地文件(绝对路径 xml 文件)
        - Classpath(相对路径 xml 文件)
        - Web 应用中依赖 servlet 或 Listener

    文件

    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:/workspace/appcontext.xml");

    Classpath

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");

    Web应用

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>context</servlet-name>
            <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

    本文出自:艺意

  • 相关阅读:
    yii required 字段去除*号
    shtml用include加载文件,apache开启SSI,及shtml压缩
    门户站点用html或shtml文件格式的原因
    Apache配置shtml
    数据库迁移工具
    SQL SERVER2005 级联删除
    MySql级联删除和更新
    js只允许输入数字和小数点
    表格中的数据为空时,td的边框不显示
    MYSQL: Cannot delete or update a parent row: a foreign key constraint fails
  • 原文地址:https://www.cnblogs.com/wkrbky/p/6368934.html
Copyright © 2020-2023  润新知