spring源码小知识点---ignoreDependencyType()和ignoreDependencyInterface()

一. 含义

ConfigurableListableBeanFactory中的2个方法:
//这个是忽略依赖该类型属性的注入;
ignoreDependencyType();
//这个是忽略该接口的实现类里的属性自动注入(忽略的是 类)
ignoreDependencyInterface();

注:这2个本质上都是xml配置时代的产物,现在基本都是springboot的自动装配,
已经不需要这2个东西了(可以直接忽略);

二. 例子

2.1 ignoreDependencyType
public class TestIgnore01 implements ApplicationContextAware {
    private Test test;
    
    public void set(Test test) {
        this.test = test;
    }
}


public class Test {
...
}

public class IgnoreAutowiringPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.ignoreDependencyType(Test.class);
    }
}

#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: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"
       default-autowire="byType">

    <bean id="testIgnore" class="com.apache.TestIgnore01"/>
    <bean id="test" class="com.apache.Test"/>
    <bean class="com.apache.IgnoreAutowiringPostProcessor"/>
</beans>

那么类TestIgnore01中的属性Test test不会被自动注入(因为在类IgnoreAutowiringPostProcessor里面已经将Test类类型给忽略掉了);

2.2 ignoreDependencyInterface
public class TestIgnore02 {
    private Test test;
    
    public void set(Test test) {
        this.test = test;
    }
}

#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: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"
       default-autowire="byType">

    <bean id="testIgnore" class="com.apache.TestIgnore02"/>
    <bean id="test" class="com.apache.Test"/>
</beans>

因为ApplicationContextAware接口的实现类会被忽略自动配置(xml配置文件里的 default-autowire="byType")(见下面源码解释),并且TestIgnore02中属性test存在set()方法(如果不存在属性的set()方法,就可以自动注入test属性值),所以TestIgnore02类里面的属性Test类不会被spring自动注入;

//AbstractApplicationContext类中prepareBeanFactory()
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	...
	// 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);
	...
}

posted @   flashOS  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示