美文网首页
核心技术-IoC容器

核心技术-IoC容器

作者: softgg | 来源:发表于2016-03-21 16:22 被阅读90次
1、IoC容器简介

控制反转IoC(Inversion of Control)又称依赖注入DI(dependency injection),由org.springframework.beans和org.springframework.context基础包构成了IoC容器,其中BeanFactory接口提供了一种先进的配置机制能够管理任何类型的对象,ApplicationContext是BeanFactory的一个子类,它更加简便的集成了Spring的AOP特性,通常情况下使用ApplicationContext,因为它提供了BeanFactory的完整超集,如果想用BeanFactory替代ApplicationContext可以参考 Section 6.16, “The BeanFactory”
Spring IoC容器工作图描述

Paste_Image.png
2、源数据(Metadata)配置

可以基于以下三种方式配置:

  • 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="..." class="..."> <!-- collaborators and configuration for this bean go here --> 
    </bean> 
    <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> 
    </bean> <!-- more bean definitions go here -->
</beans>

允许组合多个bean配置xml

<beans> 
    <import resource="services.xml"/> 
    <import resource="resources/messageSource.xml"/> 
    <import resource="/resources/themeSource.xml"/> 

    <bean id="bean1" class="..."/> 
    <bean id="bean2" class="..."/>
</beans>

使用容器加载bean

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
  <context:annotation-config/>
</beans>
@Configuration
public class AppConfig { 
    @Bean
    public MyService myService() { 
      return new MyServiceImpl(); 
    }
}

加载bean

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
MyService myService = ctx.getBean(MyService.class); 
myService.doStuff();
3、实例化bean
  • 通过构造函数实例化
<bean id="exampleBean" class="examples.ExampleBean"/>
  • 通过静态方法实例化
    java code
public class ClientService { 
    private static ClientService clientService = new ClientService(); 
    private ClientService() {} 
    public static ClientService createInstance() { 
      return clientService; 
    }
}

xml配置

<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>

静态方法带参数(通过constructor-arg传入参数)
java code

public class ExampleBean { 
    // a private constructor 
    private ExampleBean(...) { ... } 
    // a static factory method; the arguments to this method can be 
    // considered the dependencies of the bean that is returned, 
    // regardless of how those arguments are actually used. 
    public static ExampleBean createInstance ( 
      AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { 
        ExampleBean eb = new ExampleBean (...); 
        // some other operations... return eb; 
    }
}

xml 配置

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
    <constructor-arg ref="anotherExampleBean"/> 
    <constructor-arg ref="yetAnotherBean"/> 
    <constructor-arg value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  • 通过普通方法实例化
    java code
public class DefaultServiceLocator { 
    private static ClientService clientService = new ClientServiceImpl(); 
    private DefaultServiceLocator() {} 
    public ClientService createClientServiceInstance() { 
        return clientService; 
    }
}

xml配置

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator"> 
    <!-- inject any dependencies required by this locator bean -->
</bean>
<!-- the bean to be created via the factory bean -->
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
4、依赖注入
  • 基于构造器的注入
    普通类型的参数
    java code
package x.y;
public class Foo { 
    public Foo(Bar bar, Baz baz) { 
    // ... 
    }
}

xml配置

<beans> 
    <bean id="foo" class="x.y.Foo"> 
       <constructor-arg ref="bar"/> 
       <constructor-arg ref="baz"/> 
    </bean> 
    <bean id="bar" class="x.y.Bar"/> 
    <bean id="baz" class="x.y.Baz"/>
</beans>

基本数据类型的参数
java code

package examples;
public class ExampleBean { 
    // Number of years to calculate the Ultimate Answer 
    private int years; 
    // The Answer to Life, the Universe, and Everything 
    private String ultimateAnswer; 
    public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; this.ultimateAnswer = ultimateAnswer; 
    }
}

xml配置-type

<bean id="exampleBean" class="examples.ExampleBean"> 
    <constructor-arg type="int" value="7500000"/> 
    <constructor-arg type="java.lang.String" value="42"/>
</bean>

xml配置-index

<bean id="exampleBean" class="examples.ExampleBean"> 
    <constructor-arg index="0" value="7500000"/> 
    <constructor-arg index="1" value="42"/>
</bean>

xml配置-name

<bean id="exampleBean" class="examples.ExampleBean"> 
    <constructor-arg name="years" value="7500000"/> 
    <constructor-arg name="ultimateAnswer " value="42"/>
</bean>

注意,基于那么的注入,如果你的编译不是以debug方式编译,那么参数名是会变掉,这时候需要通过jdk注解指定编译后的参数名

package examples;
public class ExampleBean { 
    // Fields omitted 
    @ConstructorProperties({"years", "ultimateAnswer"})
    public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; 
      this.ultimateAnswer = ultimateAnswer; 
    }
}
  • 基于setter的注入
    java code
public class SimpleMovieLister { 
    // the SimpleMovieLister has a dependency on the MovieFinder 
    private MovieFinder movieFinder; 
    // a setter method so that the Spring container can inject a MovieFinder 
    public void setMovieFinder(MovieFinder movieFinder) { 
      this.movieFinder = movieFinder; 
    } 
    // business logic that actually uses the injected MovieFinder is omitted...
}

xml配置

<bean id="simpleMovieLister" class="examples.SimpleMovieLister"> 
    <!-- setter injection using the nested ref element --> 
    <property name="movieFinder"> 
      <ref bean="movieFinderBean"/> 
    </property>
</bean>
<bean id="movieFinderBean" class="examples.MovieFinder"/>

该方式依赖注入由容器调用setter方法将匹配参数类型的bean注入进去,所以该方式注入会在其它构造器注入之后之后注入

相关文章

  • 手把手教你写一个spring IOC容器

    摘要:spring框架的基础核心和起点毫无疑问就是IOC,IOC作为spring容器提供的核心技术,成功完成了依赖...

  • 核心技术-IoC容器

    1、IoC容器简介 控制反转IoC(Inversion of Control)又称依赖注入DI(dependenc...

  • Spring学习

    IOC (接口) 1、IOC思想基于IOC容器完成,IOC容器底层就是对象工厂; 2、Spring提供的IOC容器...

  • 2.IOC原理分析

    要想使用Spring IOC,必须要创建Spring IOC容器 ? 什么是IOC容器? 所谓的IoC容器就是指的...

  • 2018-06-08

    IOC 容器 IOC容器和beans Spring实现了IOC (Inversion of Control)(控制...

  • spring-core-1.1~1.9 IoC容器

    1. IoC容器 本章介绍Spring的控制反转,即IoC容器. 1.1 Spring IoC容器和bean简介 ...

  • 一、Spring核心篇

    第2章Spring Framework的核心:IoC容器的实现 2.1Spring IoC容器概述 1.IOC容器...

  • IoC(二)容器基本原理

    2.2.1 IoC容器的概念 IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化、定位、配置应用程序中的...

  • docker生态容器

    Docker容器生态 Docker生态:容器核心技术、平台技术、支持技术 容器核心技术:指的是container在...

  • Spring Ioc 容器原理

    IOC容器的概念 IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些...

网友评论

      本文标题:核心技术-IoC容器

      本文链接:https://www.haomeiwen.com/subject/vlvklttx.html