Spring-BeanFactory解读

https://blog.csdn.net/yangshangwei/article/details/74910935#t3

BeanFactory和ApplicationContext

Spring通过一个配置文件描述Bean和Bean之间的依赖关系,利用Java反射功能实例化Bean,并建立Bean之间的依赖关系。

Spring的IOC容器在完成这些底层工作的基础上,还提供了Bean实例缓存、生命周期管理、Bean实例代理、事件发布、资源装载等高级服务。

BeanFactory是Spring框架最核心的接口,它提供了高级IOC的配置机制。

ApplicationContext建立在BeanFactory的基础上,提供了更多面向应用的功能, 它提供了国际化支持和框架事件体系。

我们一般称BeanFactory为IoC容器,而称ApplicationContext为应用上下文,但有时候为了行文方便,我们也将ApplicationContext称为Spring容器。

对于BeanFactory 和 ApplicationContext的用途:

  • BeanFactory是Spring框架的基础设施,面向Spring本身

  • ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都可以直接使用Application而非底层的BeanFactory.


BeanFactory概述

BeanFactory是一个类的通用工厂,可以创建并管理各种类的对象。

这些被创建和管理的对象,并无特别之处,仅仅是一个POJO,Spring称这些被创建和管理的Java对象为bean.

所有可以被Spring容器实例化并管理的Java类都可以成为Bean。


解读BeanFactory的类体系结构

这里写图片描述


这里写图片描述


这里写图片描述

BeanFactory的主要方法是 getBean(String beanName). 该方法从容器中返回特定名称的Bean.

BeanFactory的功能通过其他接口得到不断的扩展,主要有以下几个接口

  • ListableBeanFactory 定义了访问容器Bean中的若干方法。 这里写图片描述

  • HierarchicalBeanFactory 父子级联IoC容器的接口,子容器可以通过 接口方法访问父容器

  • ConfigurableBeanFactory 重要接口,增强了IoC容器的可定制性

  • AutowireCapableBeanFactory 定义了容器中的Bean按照某种规则进行自动装配的方法

  • SingletonBeanRegistry 定义了允许在运行期间向容器注册单实例Bean的方法 这里写图片描述

  • BeanDefinitionRegistry:Spring配置文件中的每个<bean>节点元素在Spring中通过一个BeanDefinition对象表示,它描述了Bean的配置信息,该接口提供了向容器手工注册BeanDefinition的方法。


初始化BeanFactory

这里写图片描述


Plane.java

package com.xgj.master.ioc.beanfactory;
​
public class Plane {
​
    private String brand;
    private String color;
    private int maxSpeed;
​
    public String getBrand() {
        return brand;
    }
​
    public void setBrand(String brand) {
        this.brand = brand;
    }
​
    public String getColor() {
        return color;
    }
​
    public void setColor(String color) {
        this.color = color;
    }
​
    public int getMaxSpeed() {
        return maxSpeed;
    }
​
    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
​
    public void introduce(){
        System.out.println("Plane brand:" + brand + " ,Color:" + color + ",maxSpeed:" + maxSpeed);
    }
}
12345678910111213141516171819202122232425262728293031323334353637

bean-plane.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" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
​
    <bean id="plane" class="com.xgj.master.ioc.beanfactory.Plane"
        p:brand="A380" p:color="Blue" p:maxSpeed="700" />
​
</beans>123456789101112

BeanFactoryTest.java

package com.xgj.master.ioc.beanfactory;
​
import java.io.IOException;
​
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
​
​
public class BeanFactoryTest {
​
    public static void main(String[] args) throws IOException {
​
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
​
        Resource resource = resolver.getResource("classpath:com/xgj/master/ioc/beanfactory/bean-plane.xml");
​
        System.out.println(resource.getURL());
​
        //BeanFactory factory = new XmlBeanFactory(resource);  被废弃,不建议使用
​
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
​
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
​
        reader.loadBeanDefinitions(resource);
​
        System.out.println("inti BeanFactory successfully");
​
        Plane plane = beanFactory.getBean("plane",Plane.class);
​
        System.out.println("Plane Bean  is ready to use");
​
        plane.introduce();
​
​
    }
}
1234567891011121314151617181920212223242526272829303132333435363738394041

这里写图片描述

解读:

XmlBeanDefinitionReader 通过Resource装载Spring配置信息并启动IOC容器,然后就可以通过BeanFactory#getBean(beanName)方法从IOC容器中获取bean。

通过BeanFactory启动IOC,初始化动作发生在第一调用时。

对于单实例singleton的bean来将,BeanFactory会缓存bean实例, 所以第二次使用getBean()时,将直接从IOC容器的缓存中获取Bean实例。

Spring在DefaultSingletonBeanRegistry类中提供了一个用户缓存单实例bean的缓存器, 是一个HashMap实现的缓存器。 beanName作为键保存在map中。

这里写图片描述

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值