spring学习(六)——6.1 spring的ApplicationContext

参考文章:

http://www.iocoder.cn/

虽然bean的创建都是使用BeanFactroy,但是生产环境我们通常会选择 ApplicationContext

和BeanFactory 区别

  • 继承 MessageSource,提供国际化的标准访问策略。
  • 继承 ApplicationEventPublisher ,提供强大的事件机制。
  • 扩展 ResourceLoader,可以用来加载多个 Resource,可以灵活访问不同的资源。
  • 对 Web 应用的支持。

ApplicationContext类图

结构图

BeanFactory:Spring 管理 Bean 的顶层接口,一个简易版的 Spring 容器

ApplicationEventPublisher:用于封装事件发布功能的接口,向事件监听器(Listener)发送事件消息

ResourceLoader:Spring 加载资源的顶层接口

MessageSource:解析 message 的策略接口,用于支撑国际化等功能

EnvironmentCapable:用于获取 Environment 的接口

 

ApplicationContext 的子接口

两个直接子类:WebApplicationContext 和 ConfigurableApplicationContext

WebApplicationContext

该接口只有一个 #getServletContext() 方法,用于给 Servlet 提供上下文信息。

public interface WebApplicationContext extends ApplicationContext {

	/**
	 * Return the standard Servlet API ServletContext for this application.
	 */
	@Nullable
	ServletContext getServletContext();

}

ConfigurableApplicationContext

该接口继承了Lifecycle执行生命周期的管理,Closeable资源释放

public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {

	/**
	 * Any number of these characters are considered delimiters between
	 * multiple context config paths in a single String value.
	 * @see org.springframework.context.support.AbstractXmlApplicationContext#setConfigLocation
	 * @see org.springframework.web.context.ContextLoader#CONFIG_LOCATION_PARAM
	 * @see org.springframework.web.servlet.FrameworkServlet#setContextConfigLocation
	 */
	String CONFIG_LOCATION_DELIMITERS = ",; \t\n";

	/**
	 * Name of the ConversionService bean in the factory.
	 * If none is supplied, default conversion rules apply.
	 * @since 3.0
	 * @see org.springframework.core.convert.ConversionService
	 */
	String CONVERSION_SERVICE_BEAN_NAME = "conversionService";

	/**
	 * Name of the LoadTimeWeaver bean in the factory. If such a bean is supplied,
	 * the context will use a temporary ClassLoader for type matching, in order
	 * to allow the LoadTimeWeaver to process all actual bean classes.
	 * @since 2.5
	 * @see org.springframework.instrument.classloading.LoadTimeWeaver
	 */
	String LOAD_TIME_WEAVER_BEAN_NAME = "loadTimeWeaver";

	/**
	 * Name of the {@link Environment} bean in the factory.
	 * @since 3.1
	 */
	String ENVIRONMENT_BEAN_NAME = "environment";

	/**
	 * Name of the System properties bean in the factory.
	 * @see java.lang.System#getProperties()
	 */
	String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties";

	/**
	 * Name of the System environment bean in the factory.
	 * @see java.lang.System#getenv()
	 */
	String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";


	 // 为 ApplicationContext 设置唯一 ID
    void setId(String id);
    
    // 为 ApplicationContext 设置 parent
    // 父类不应该被修改:如果创建的对象不可用时,则应该在构造函数外部设置它
    void setParent(@Nullable ApplicationContext parent);
    
    // 设置 Environment
    void setEnvironment(ConfigurableEnvironment environment);
    
    // 获取 Environment
    @Override
    ConfigurableEnvironment getEnvironment();
    
    // 添加 BeanFactoryPostProcessor
    void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor);
    
    // 添加 ApplicationListener
    void addApplicationListener(ApplicationListener<?> listener);
    
    // 添加 ProtocolResolver
    void addProtocolResolver(ProtocolResolver resolver);
    
    // 加载或者刷新配置
    // 这是一个非常重要的方法
    void refresh() throws BeansException, IllegalStateException;
    
    // 注册 shutdown hook
    void registerShutdownHook();
    
    // 关闭 ApplicationContext
    @Override
    void close();
    
    // ApplicationContext 是否处于激活状态
    boolean isActive();
    
    // 获取当前上下文的 BeanFactory
    ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

}

ConfigurableWebApplicationContext

此接口是对WebApplicationContext和ConfigurableApplicationContext的整合

public interface ConfigurableWebApplicationContext extends WebApplicationContext, ConfigurableApplicationContext

 

实现类

针对某一个实现类的分析ClassPathXmlApplicationContext

类关系图

实现接口个具体功能

  • BeanFactory:Spring 容器 Bean 的管理
  • MessageSource:管理 message ,实现国际化等功能
  • ApplicationEventPublisher:事件发布
  • ResourcePatternResolver:资源加载
  • EnvironmentCapable:系统 Environment(profile + Properties) 相关
  • Lifecycle:管理生命周期
  • Closable:关闭,释放资源
  • InitializingBean:自定义初始化
  • BeanNameAware:设置 beanName 的 Aware 接口

MessageSource

MessageSource 定义了获取 message 的策略方法

ApplicationEventPublisher

ApplicationEventPublisher ,用于封装事件发布功能的接口,向事件监听器(Listener)发送事件消息。

ResourcePatternResolver

ResourcePatternResolver 接口继承 ResourceLoader 接口,为将 location 解析为 Resource 对象的策略接口。

EnvironmentCapable

提供当前系统环境 Environment 组件。提供了一个 #getEnvironment() 方法,用于返回 Environment 实例对象。

Lifecycle

Lifecycle ,一个用于管理声明周期的接口。

其接口内容

public interface Lifecycle {

	/**
	 *  
	 */
	void start();

	/**
	 *  
	 */
	void stop();

	/**
	 *  
	 */
	boolean isRunning();

}

Closable

Closable 接口用于关闭和释放资源,提供了 #close() 方法,以释放对象所持有的资源。

InitializingBean

InitializingBean 为 Bean 提供了初始化方法的方式,它提供的 #afterPropertiesSet() 方法,用于执行初始化动作。

BeanNameAware

BeanNameAware ,设置 Bean Name 的接口。

小结

ApplicationContext 对整套接口提供了大部分的默认实现,将其中“不易变动”的部分进行了封装,通过“组合”的方式将“容易变动”的功能委托给其他类来实现,同时利用模板方法模式将一些方法的实现开放出去由子类实现,从而实现“对扩展开放,对修改封闭”的设计原则。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大·风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值