BeanFactory和ApplicationContext的关系

Crq
Crq
Crq
218
文章
0
评论
2022年5月6日09:23:50
评论
534 2900字阅读9分40秒
摘要

使用版本为 Spring Framework 5.2.2.RELEASE,来探索BeanFactory和ApplicationContext的关系。

开始

首先创建了Repository类,有两个属性,类型为 BeanFactory 和 ApplicationContext

public class Repository {
private BeanFactory beanFactory;
private ApplicationContext applicationContext;
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
}

创建配置bean的xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="repository" class="ioc.overview.repository.Repository" autowire="byType"></bean>
</beans>

创建测试类:

public class DependencyInjectionDemo {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/injection-context.xml");
Repository repository = beanFactory.getBean("userRepository", Repository.class);
System.out.println("injected beanFactory == main beanFactory: " + (repository.getBeanFactory() == beanFactory));
System.out.println("injected applicationContext == main beanFactory: " + (repository.getApplicationContext() == beanFactory));
}
}

打印如下:

injected beanFactory == main beanFactory: false
injected applicationContext == main beanFactory: true
为什么 beanFactory 不等于 beanFactory 呢?

看看类的继承关系

ClassPathXmlApplicationContext继承自 AbstractApplicationContext 。

// AbstractApplicationContext 实现了 ApplicationContext 接口
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
}
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
}
// ApplicationContext 是 BeanFactory的子接口
public interface ApplicationContext extends ListableBeanFactory...
public interface ListableBeanFactory extends BeanFactory {}

再来看 ConfigurableApplicationContext 中,声明了一个获取 beanFactory 的方法:

ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

自己就是 beanFactory,为什么还要声明这个方法呢?

接着,来看这个方法的实现:

AbstractRefreshableApplicationContext#getBeanFactory
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
/** Bean factory for this context. */
@Nullable
private DefaultListableBeanFactory beanFactory;
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
synchronized (this.beanFactoryMonitor) {
if (this.beanFactory == null) {
throw new IllegalStateException("BeanFactory not initialized or already closed - " + "call 'refresh' before accessing beans via the ApplicationContext");
}
return this.beanFactory;
}
}

再来看看 getBean 的实现,他使用了组合的方式,使用 beanFactory 对象进行获取 bean。

@Override
public  T getBean(String name, Class requiredType) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name, requiredType);
}
总结

BeanFactory 是底层的Ioc容器,ApplicationContext 继承了它,并增加了一些特性。在实现方面,ApplicationContext 组合了一个 BeanFactory 的实现,使用这个实现对象来完成一些操作。

weinxin
我的微信
这是我的微信扫一扫
Crq
  • 本文由 发表于 2022年5月6日09:23:50
  • 转载请注明:https://www.cncrq.com/10166.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: