Spring学习-04:BeanFactory和ApplicationContext

在Spring学习-03中,介绍了ApplicationContext的两个子类,其中类之间的继承关系如下图所示:

Spring学习-04:BeanFactory和ApplicationContext_第1张图片

ApplicationContext间接的继承了BeanFactory类。

在myeclipse中追踪源代码:

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

	/**
	 * Return the unique id of this application context.
	 * @return the unique id of the context, or null if none
	 */
	String getId();

	/**
	 * Return a name for the deployed application that this context belongs to.
	 * @return a name for the deployed application, or the empty String by default
	 */
	String getApplicationName();

	/**
	 * Return a friendly name for this context.
	 * @return a display name for this context (never null)
	*/
	String getDisplayName();

	/**
	 * Return the timestamp when this context was first loaded.
	 * @return the timestamp (ms) when this context was first loaded
	 */
	long getStartupDate();

	/**
	 * Return the parent context, or null if there is no parent
	 * and this is the root of the context hierarchy.
	 * @return the parent context, or null if there is no parent
	 */
	ApplicationContext getParent();

	/**
	 * Expose AutowireCapableBeanFactory functionality for this context.
	 * 

This is not typically used by application code, except for the purpose * of initializing bean instances that live outside the application context, * applying the Spring bean lifecycle (fully or partly) to them. *

Alternatively, the internal BeanFactory exposed by the * {@link ConfigurableApplicationContext} interface offers access to the * AutowireCapableBeanFactory interface too. The present method mainly * serves as convenient, specific facility on the ApplicationContext * interface itself. * @return the AutowireCapableBeanFactory for this context * @throws IllegalStateException if the context does not support * the AutowireCapableBeanFactory interface or does not hold an autowire-capable * bean factory yet (usually if refresh() has never been called) * @see ConfigurableApplicationContext#refresh() * @see ConfigurableApplicationContext#getBeanFactory() */ AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException; }

其中,继续追踪HierarchicalBeanFactory接口,发现:

public interface HierarchicalBeanFactory extends BeanFactory {
	
	/**
	 * Return the parent bean factory, or null if there is none.
	 */
	BeanFactory getParentBeanFactory();

	/**
	 * Return whether the local bean factory contains a bean of the given name,
	 * ignoring beans defined in ancestor contexts.
	 * 

This is an alternative to containsBean, ignoring a bean * of the given name from an ancestor bean factory. * @param name the name of the bean to query * @return whether a bean with the given name is defined in the local factory * @see org.springframework.beans.factory.BeanFactory#containsBean */ boolean containsLocalBean(String name); }

BeanFactory和ApplicationContext区别:

1、BeanFactory采用了延迟加载,在使用到这个类的时候,也就是调用getBean()方法的时候,才会加载这个类。早期开发的时候,也使用BeanFactory来加载配置文件。

@Test
	public void demo4(){
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
		HelloService helloService=(HelloService)beanFactory.getBean("userService");
		helloService.sayHello();
	}

2、ApplicationContext类加载配置文件的时候,创建所有的类。

并且,ApplicationContext对BeanFactory提供了扩展:国际化处理,事件传递,Bean自动装配,各种不同应用层的Context实现。


你可能感兴趣的:(Spring,Spring学习之路)