Spring IoC是Spring Framework的核心。
控制反转和依赖注入:
早在2004年,Martin Fowler就提出了“哪些方面的控制被反转了?”,后来得出的结论是:依赖对象的获得被反转了。基于这个结论,他为控制反转创造了一个更好的名字:依赖注入。
不过我们平时会同时说到控制反转和依赖注入两个概念,控制反转指的就是把对象的获得交给容器,这样就反转了。依赖注入指的是容器把对象之间的依赖注入进去。
控制反转的实现有多种方式,可以在对象生成或初始化的时候,直接将数据注入到对象中。也可以把对象的引用注入进去。
Spring IoC:
Spring IoC容器的设计,最关键的就是两个接口,BeanFactory和ApplicationContext。
BeanFactory接口定义了IoC容器基本的功能规范,ApplicationContext在BeanFactory的基础上,增加了许多面向框架的特性,对应用环境做了很多适配,是容器的高级形态。
在Idea中,通过control+H快捷键,可以看到BeanFactory的继承情况。
网上也可以搜到Spring IoC容器的接口设计图。
在Spring IoC容器中,有两条线路,一条是BeanFactory,一条是ApplicationContext。BeanFactory这条线是简单的IoC容器,ApplicationContext继承了ListableBeanFactory,具备了简单IoC容器的功能,同时又继承了MessageSource、ResourcePatternResolver、ApplicationEventPublisher等接口,拥有了更高级的IoC容器特性。
下面先看下BeanFactory这条线:
BeanFactory算是IoC容器中最顶级的一个接口了,它定义了IoC容器最基本的形式,提供了最基本的功能。比如,接口中定义了getBean方法,用来获取IoC容器中的一个Bean。
下面是BeanFactory的源码:
public interface BeanFactory {
/**
* Used to dereference a {@link FactoryBean} instance and distinguish it from
* beans <i>created</i> by the FactoryBean. For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*/
String FACTORY_BEAN_PREFIX = "&";
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>This method allows a Spring BeanFactory to be used as a replacement for the
* Singleton or Prototype design pattern. Callers may retain references to
* returned objects in the case of Singleton beans.
* <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to retrieve
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no bean definition
* with the specified name
* @throws BeansException if the bean could not be obtained
*/
Object getBean(String name) throws BeansException;
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Behaves the same as {@link #getBean(String)}, but provides a measure of type
* safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the
* required type. This means that ClassCastException can't be thrown on casting
* the result correctly, as can happen with {@link #getBean(String)}.
* <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to retrieve
* @param requiredType type the bean must match. Can be an interface or superclass
* of the actual class, or {@code null} for any match. For example, if the value
* is {@code Object.class}, this method will succeed whatever the class of the
* returned instance.
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanNotOfRequiredTypeException if the bean is not of the required type
* @throws BeansException if the bean could n