名詞
Bean
java類的代理透典。在Spring中晴楔,那些組成你應(yīng)用程序的主體(backbone)及由Spring IoC容器所管理的對象,被稱之為bean峭咒。 簡單地講税弃,bean就是由Spring容器初始化、裝配及管理的對象讹语,除此之外钙皮,bean就與應(yīng)用程序中的其他對象沒有什么區(qū)別了。BeanFactory
spring ioc的容器顽决,負(fù)責(zé)對Bean進(jìn)行管理ApplicationContext
對BeanFactory
功能的增強(qiáng)IOC
傳統(tǒng)上由程序代碼直接操控的對象的調(diào)用權(quán)交給容器,現(xiàn)在通過容器來實(shí)現(xiàn)對象組件的裝配和管理导匣。所謂的“控制反轉(zhuǎn)”概念就是對組件對象控制權(quán)的轉(zhuǎn)移才菠,從程序代碼本身轉(zhuǎn)移到了外部容器。
Demo
測試代碼ClassPathXmlApplicationContextTests.testAliasThatOverridesParent
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(FQ_SIMPLE_CONTEXT);
Object someMs = ctx.getBean("someMessageSource");
ClassPathXmlApplicationContext.java
, 用于管理xml描述的bean
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
}
AbstractApplicationContext.java
, bean容器的基類
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
@Override
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();
......
}
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
@Override
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
}
AbstractRefreshableApplicationContext.java
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
AbstractXmlApplicationContext.java
public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}
}