一. 含义
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);
...
}
标签:知识点,beanFactory,ignoreDependencyType,ignoreDependencyInterface,public,Test,源码,te
From: https://www.cnblogs.com/scott1440298847/p/17232065.html