• IOC及Bean容器


    1. 接口及面向接口编程

    1.1. 接口

    用于沟通的中介物的抽象化

    实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式

    对应JAVA接口即声明,声明了哪些方法是对外公开提供的

    在JAVA8中,接口可以拥有方法体

    1.2. 面向接口编程

    结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类

    接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要

    “面向接口编程”中的“接口”是用于隐藏具体实现和实现多态性的组件

    1 package com.imooc.ioc.interfaces;
    2 
    3 public class Main {
    4     public static void main(String[] args) {        
    5         OneInterface oif = new OneInterfaceImpl();        
    6         System.out.println(oif.hello("word"));        
    7     }    
    8 }
    1 package com.imooc.ioc.interfaces;
    2 
    3 public class OneInterfaceImpl implements OneInterface {
    4     public String hello(String word) {
    5         return "Word from interface"OneInterface":" + word;
    6     }
    7 }
    1 package com.imooc.ioc.interfaces;
    2 
    3 public interface OneInterface {
    4     String hello(String word);
    5 }

     

    2. 什么是IOC

    Ioc:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护

    DI(依赖注入)是其一种实现方式

    目的:创建对象并且组装对象之间的关系

    3. Spring的Bean配置

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <beans xmlns="http://www.springframework.org/schema/beans" 
    3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    4   xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd">
    5 
    6   <bean id="oneInterface" class="com.imooc.ioc.interface.OneInterfaceImpl"></bean>
    7 </beans>

    4. 单元测试

    ——下载junit-*.jar并引入工程

    ——创建UnitTestBase类,完成对Spring配置文件的加载、销毁

    ——所有的单元测试类都继承自UnitTestBase,通过它的getBean方法获取想要得到的对象

    ——子类(具体执行单元测试的类)加注解:@RunWith(BlockJUnit4ClassRunner.class)

    ——单元测试方法加注解:@Test

    ——右键选择要执行的单元测试方法执行或者执行一个类的全部单元测试方法

    5. Bean的初始化

    基础:两个包

    ——org.springframework.beans

    ——org.springframework.context

    ——BeanFactory提供配置结构和基本功能,加载并初始化Bean

    ——ApplicationContext保存了Bean对象并在Spring中被广泛使用

    方式,ApplicationContext

    ——本地文件

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

    ——Classpath

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

    ——Web应用中依赖Servlet或Listener

    1 <listener>
    2   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    3 </listener>
    4 <servlet>
    5   <servlet-name>context</servlet>
    6   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    7   <load-on-startup>1</load-on-satrtup>
    8 <servlet>

    6. Spring的常用注入方式

    Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为

    常用的两种注入方式

    6.1. 设值注入

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     4   xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6   <bean id="injectionService"  class="com.imooc.ioc.injection.service.InjectionServiceImpl">
     7     <property name="injectionDAO" ref="injectionDAO"></property>
     8   </bean>
     9   <bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>
    10 </beans>

    6.2. 构造注入

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     4   xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6   <bean id="injectionService"  class="com.imooc.ioc.injection.service.InjectionServiceImpl">
     7     <constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg>
     8   </bean>
     9   <bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>
    10 </beans>
  • 相关阅读:
    分布式理论基础(三)时间、时钟和事件顺序
    分布式理论基础(二)选举、多数派和租约
    分布式理论基础(一)一致性及解决一致性的两种方式:2PC和3PC
    spark入门(三)键值对操作
    spark入门(二)RDD基础操作
    Python的Flask框架入门-Ubuntu
    Python __str__(self)和__unicode__(self)
    Windows下安装和使用MongoDB
    Virtualenv介绍
    python的import与from…import的区别
  • 原文地址:https://www.cnblogs.com/BlackList-Sakura/p/4333986.html
Copyright © 2020-2023  润新知