博客
关于我
Spring里面的ioc拓展
阅读量:355 次
发布时间:2019-03-04

本文共 4079 字,大约阅读时间需要 13 分钟。

在常用的一些spring ioc配置中,对于bean的依赖注入是常规项目中最常见功能了。除此之外,Spring还提供有一些比较灵活的功能点用于对Spring容器里面的组件进行扩充和操作。

 

IOC

  1. Bean定义的定位,Bean 可能定义在XML中,或者一个注解,或者其他形式。这些都被用Resource来定位, 读取Resource获取BeanDefinition 注册到 Bean定义注册表中。

  2. 第一次向容器getBean操作会触发Bean的创建过程,实列化一个Bean时 ,根据BeanDefinition中类信息等实列化Bean.

  3. 将实列化的Bean放到单列Bean缓存内.

  4. 此后再次获取向容器getBean就会从缓存中获取

  5. ioc容器本质就是一个map结构

Spring IOC :

对于ioc容器的一些处理器

 

BeanDefinitionRegistryPostProcessor

BeanDefinitionRegistryPostProcessor主要功能时提供给开发人员在对bean注入ioc容器之前进行额外操作的一个处理器。例如一些比较特殊的bean需要手动注入到spring的ioc容器里面去的时候,可以采用这个处理器来进行统一注入。

PS:下列代码均是在springboot 2.0环境下运行的

package com.sise.config;import com.sise.annotation.MyBean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;import org.springframework.core.type.filter.AnnotationTypeFilter;import org.springframework.stereotype.Component;/** * 手动设置注入相应的bean到ioc容器里边 * * @author idea * @data 2019/3/18 */@Componentpublic class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {    @Override    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {        System.out.println("动态加载bean进入ioc容器里面");        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);        //扫描所有包含指定注解的bean加入到ioc里面        scanner.addIncludeFilter(new AnnotationTypeFilter(MyBean.class));        scanner.scan("com.sise.bean");    }    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {    }}

BeanFactoryPostProcessor 

BeanFactoryPostProcessor 则是一个专门用于对bean做加入ioc容器之前进行处理的一个处理器,例如说一些bean注入的校验,初始化操作。

package com.sise.config;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.stereotype.Component;/** * 在加入ioc容器之前,对bean做处理 * * @author idea * @data 2019/3/18 */@Componentpublic class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {        System.out.println("在加入ioc容器之前需要进行的处理");        String[] beanNameArr = beanFactory.getBeanDefinitionNames();        for (String name : beanNameArr) {            //这里的name是采用驼峰式命名的规则            if (name.equals("user")) {                //这里面可以做一些对于bean加载时候所需要进行的限制操作                System.out.println("加载了user类对象");            }        }    }}

 

BeanPostProcessor

BeanPostProcessor是专门处理已经注入ioc容器里面的bean的初始化之前和初始化之后相关操作的一款处理器。例如说对于某个带有特定注解的类,在进行初始化之后,需要对相应的属性进行额外处理

package com.sise.config;import com.sise.annotation.MyBean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.stereotype.Component;/** * 在每个ioc容器里面的bean被初始化之前和初始化之后 * * @author idea * @data 2019/3/18 */@Componentpublic class MyBeanPostProcessor implements BeanPostProcessor {    @Override    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {        if (bean.getClass().getAnnotation(MyBean.class) != null) {            System.out.println("beanName:" + beanName);        }        return bean;    }    @Override    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {        if (bean.getClass().getAnnotation(MyBean.class) != null) {            System.out.println("beanName:" + beanName);        }        return bean;    }}

BeanPostProcessorBeanFactoryPostProcessor 的功能有点类似,但是BeanFactoryPostProcessor 更多的是处理在加入ioc容器之前的操作,BeanPostProcessor则是处理加入ioc容器之后,bean初始化前后的操作。

结合Spring容器里面的这些ioc扩展功能,随着整体项目业务的变化,开发者可以进行一些灵活的变迁操作。例如说在一些bean进行初始化加载的时候做一些限制操作,又或者说在一些基本的bean装载过程中做一定的拦截操作。

 

转载地址:http://yzte.baihongyu.com/

你可能感兴趣的文章
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>