• ignoreDependencyInterface与registerResolvableDependency


    AbstractApplicationContext#refresh方法有一个prepareBeanFactory

    prepareBeanFactory方法

    对beanFactory做准备工作

    protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    		// Tell the internal bean factory to use the context's class loader etc.
    		beanFactory.setBeanClassLoader(getClassLoader());
    		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    
    		// Configure the bean factory with context callbacks.
    		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
    
    		// BeanFactory interface not registered as resolvable type in a plain factory.
    		// MessageSource registered (and found for autowiring) as a bean.
    		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    		beanFactory.registerResolvableDependency(ApplicationContext.class, this);
    
    		// Register early post-processor for detecting inner beans as ApplicationListeners.
    		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
    
    		// Detect a LoadTimeWeaver and prepare for weaving, if found.
    		if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
    			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
    			// Set a temporary ClassLoader for type matching.
    			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    		}
    
    		// Register default environment beans.
    		if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
    			beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    		}
    		if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
    			beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    		}
    		if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
    			beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    		}
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    这个方法其实就是对beanfactory的一些属性设置,以及加入一些后续会用到的bean
    这里看ignoreDependencyInterface的作用是什么
    我了解到BeanFactory接口中有个方法叫ignoreDependencyInterface。从官方文档的“字面”来看,其作用指定自动装配(autowiring)的时候忽略的接口。还有一个很相似的方法叫ignoreDependencyType,同样其官方字面意思是指自动装配(autowiring)的时候忽略的类。
    究竟这两个方法是不是我们的理解相同呢?真的可以让指定的接口和类在自动装配的时候被忽略?有没有注意不到的坑?

    /**
    * Ignore the given dependency interface for autowiring.
    * 

    This will typically be used by application contexts to register * dependencies that are resolved in other ways, like BeanFactory through * BeanFactoryAware or ApplicationContext through ApplicationContextAware. *

    By default, only the BeanFactoryAware interface is ignored. * For further types to ignore, invoke this method for each type. * @param ifc the dependency interface to ignore * @see org.springframework.beans.factory.BeanFactoryAware * @see org.springframework.context.ApplicationContextAware */ void ignoreDependencyInterface(Class<?> ifc); /** * Ignore the given dependency type for autowiring: * for example, String. Default is none. * @param type the dependency type to ignore */ void ignoreDependencyType(Class<?> type);

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    看上面的注释不是很明白,ignoreDependencyType和ignoreDependencyInterface,从方法名来看一个是忽略某些类的依赖,一个是忽略某些接口的依赖。什么意思呢?在Spring中我们经常使用的是面向接口的编程,也就是在自动注入中,如果发现接口或者类被ignoreDependency了,就不会自动注入了。比如说你不能自动注入BeanFactory和ApplicationContext,它们必须通过BeanFactoryAware和ApplicationContextAware来注入

    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    
    • 1

    这句代码表示的是
    在这里插入图片描述

    set方法内的类型,自动装配时会被忽略掉,想要注入必须通过bean继承EnvironmentAware接口。

    registerResolvableDependency作用(功能上就像@Primary)
    该方法在springIOC初始化的时候的使用,针对根据类型注入的情况
    由于BeanFactory会有很多实现,如果我们在Spring中直接想注入BeanFactory类型的对象,不这么做的话会报错的。Spring通过这个registerResolvableDependency()方法很好的解决了这个问题,当你想要注入BeanFactory对象的时候,Spring在初始化时期就帮我们指定了要注入哪个对象
    该方法的主要作用就是指定该类型接口,如果外部要注入该类型接口的对象,则会注入我们指定的对象,而不会去管其他接口实现类

  • 相关阅读:
    Android焦点之SurfaceFlinger的apply
    Python分享之特殊方法与多范式
    springboot下添加日志模块和设置日志文件输出
    【07】FISCOBCOS一键部署前的准备工作ubuntu安装,mysql,python,PyMySQL,java
    【Python零基础入门篇 · 27】:文件操作
    Cocos creator实现《滑雪趣挑战》滑雪小游戏资源及代码
    SpringBoot整合篇 04、Springboot整合Redis
    JT808协议介绍 --- 格林恩德 CR202 RTK 高精度车载定位器协议解读
    如何使用grpc从服务端像向客户端传递错误信息
    java计算机毕业设计springboot+vue废弃资源回收系统
  • 原文地址:https://blog.csdn.net/qq_37436172/article/details/127833905