1.spring中applicationContext继承关系

ApplicationContext是spring中的顶级核心接口,ApplicationContext的实现类一定是BeanFactory的实现类,需要获取到环境变量、事件发布、资源文件解析,需要支持国际化,
先看下这个接口的继承关系及各个父接口提供的功能

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

继承的接口方法及注释详情

EnvironmentCapable 
  - getEnvironment()  
/**
* This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. 
* 这个接口被拥有一系列通过字符串名称定义的beandefinitions的类实现
* Spring's Dependency Injection functionality is implemented using this BeanFactory interface and its subinterfaces.
* spring的依赖注入功能就是通过这个BeanFactory和它的子接口实现的。
* Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML document), and use the {@code *org.springframework.beans}package to configure the beans.
* 通常一个BeanFactory会加载存储载配置文件中的Bean定义信息,然后使用beans包配置这些bean
 * There are no constraints on how the definitions could be stored: LDAP, RDBMS, XML, properties file, etc. Implementations are * encouraged to support references amongst beans (Dependency Injection)
* 关于bean定义信息如何被存储是没有约束的,如LDAP,RDBMS,XML,properties文件等等。实现类支持bean之间的互相引用是最好的。
* <p>Bean factory implementations should support the standard bean lifecycle interfaces
 * as far as possible. The full set of initialization methods and their standard order is:
* BeanFactory接口的实现应当尽可能支持标准的bean生命周期,完整的初始化方法集及调用顺序如下: 
 * <ol>
 * <li>BeanNameAware's {@code setBeanName}
 * <li>BeanClassLoaderAware's {@code setBeanClassLoader}
 * <li>BeanFactoryAware's {@code setBeanFactory}
 * <li>EnvironmentAware's {@code setEnvironment}
 * <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
 * <li>ResourceLoaderAware's {@code setResourceLoader}
 * (only applicable when running in an application context)
 * <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
 * (only applicable when running in an application context)
 * <li>MessageSourceAware's {@code setMessageSource}
 * (only applicable when running in an application context)
 * <li>ApplicationContextAware's {@code setApplicationContext}
 * (only applicable when running in an application context)
 * <li>ServletContextAware's {@code setServletContext}
 * (only applicable when running in a web application context)
 * <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors
 * <li>InitializingBean's {@code afterPropertiesSet}
 * <li>a custom init-method definition
 * <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors
 * </ol>
 *
 * <p>On shutdown of a bean factory, the following lifecycle methods apply:
 * <ol>
 * <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
 * <li>DisposableBean's {@code destroy}
 * <li>a custom destroy-method definition
 * </ol>
*/
public interface BeanFactory 
  - Object getBean(String name) throws BeansException;
  - <T> T getBean(String name, Class<T> requiredType) throws BeansException;
  - Object getBean(String name, Object... args) throws BeansException;
  - <T> T getBean(Class<T> requiredType) throws BeansException;
  - <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
  - <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
  - <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
  - boolean containsBean(String name);
  - boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
  - boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
  - boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
  - boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
  - Class<?> getType(String name) throws NoSuchBeanDefinitionException;
  - Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
  - String[] getAliases(String name);
  
public interface ListableBeanFactory extends BeanFactory 
  - boolean containsBeanDefinition(String beanName);
  - String[] getBeanDefinitionNames();
  - <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType, boolean allowEagerInit);
  - <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType, boolean allowEagerInit);
  - String[] getBeanNamesForType(ResolvableType type);
  - String[] getBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit);
  - String[] getBeanNamesForType(@Nullable Class<?> type);
  - String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);
  - <T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException;
  - <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
			throws BeansException;
  - String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
  - Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
  - <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
			throws NoSuchBeanDefinitionException;

public interface HierarchicalBeanFactory extends BeanFactory 
  - BeanFactory getParentBeanFactory();
  - boolean containsLocalBean(String name);   不考虑父或者祖先容器判断bean是否存在
   
public interface MessageSource  信息解析和国际化使用
  - String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
  - String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
  - String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
  
public interface ApplicationEventPublisher 事件发布器,作为applicationContext的父接口,函数式接口
  - default void publishEvent(ApplicationEvent event) {
		publishEvent((Object) event);
	}
  - void publishEvent(Object event);
public interface ResourcePatternResolver 类路径下的资源解析器
  - Resource[] getResources(String locationPattern) throws IOException;

对应的类图关系为:
ApplicationContext接口向上继承关系图

SpringApplicationContext提供了一种机制来传递事件。事件是应用程序不同组件之间的通信方式,通过事件,一个组件可以通知其他组件某个特定事件的发生,其他组件可以对该事件进行监听并做出相应的处理。 ApplicationContext使用了观察者模式实现事件传递。当某个组件通过ApplicationContext发布一个事件时,ApplicationContext会将该事件派发给所有对该事件感兴趣的组件。感兴趣的组件需要实现ApplicationListener接口并在ApplicationContext进行注册,以便接收到事件。 事件类通常继承ApplicationEvent,并通过构造函数或setter方法传递事件的相关信息。当某个事件发生后,发布该事件的组件会调用ApplicationContext的publishEvent方法,并将事件作为参数传递给该方法。然后,ApplicationContext会遍历所有注册的监听器,将事件传递给所有对该事件感兴趣的监听器进行处理。 事件的传递过程是同步的,即发布事件的组件会等待所有监听器对事件的处理完成才会继续执行。这种同步传递机制保证了事件的正确处理顺序,避免了可能出现的并发问题。 通过使用ApplicationContext对事件进行传递,组件之间可以实现解耦,降低了代码的复杂性。例如,当某个对象状态发生变化时,可以通过事件通知其他关联对象进行相应的更新操作,而不需要显式地调用其他对象的方法。这样,系统的可扩展性和灵活性都得到了提升。 总之,ApplicationContextSpring提供了一种方便的机制来传递事件,帮助不同组件之间进行通信和解耦操作。通过事件的发布和监听,组件之间可以实现松耦合的关系,提高系统的灵活性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

泽济天下

你的鼓励是我最大的动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值