学了如何使用spring框架后,发现了神奇的ioc容器。但是,springioc容器的本质到底是什么?起先猜想:容器应该是一种集合,如此众多的集合哪一个才能适合它呢?
于是,我创建了一个简单的spring项目,用的是5.0.2版本的
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
包目录如下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b0UgUUSG-1595502548291)(http://116.62.121.147/upload/2020/07/image-82143900e0b94e0fa0e5752c25474c91.png)]
在UserController中模拟controller层,我使用了main方法代替
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("springapplication.xml");
UserService service = (UserService) ac.getBean("UserService");
System.out.println(service.findAll());
}
给创建容器的这行代码打上断点进行debug调试:
ApplicationContext ac = new ClassPathXmlApplicationContext("springapplication.xml");
我们发现ClassPathXmlApplicationContext构造方法调用了另一个构造方法
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}
在构造方法中调用了父类AbstractApplicationContext的refresh()方法
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
refresh()方法是spring框架中比较重要的方法,创建了beanfactory工厂,设置它的属性。最后调用finishBeanFactoryInitialization(beanFactory)方法完成实例化。
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
finishBeanFactoryInitialization(beanFactory)方法创建单例对象
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
...
beanFactory.preInstantiateSingletons();
}
再调用getbean方法
@Override
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isDebugEnabled()) {
...
}
else {
getBean(beanName);
}
}
}
调用本类中的doget方法
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
Object sharedInstance = getSingleton(beanName);
...
}
使用getSingleton(beanName)方法返回容器中的代理对象。
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
......
}
return singletonObject;
}
最后我们发现,储存这些实例化对象的ioc容器实际上是一个ConcurrentHashMap
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);