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);
...
}
分类:
spring小知识点
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现