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;
对应的类图关系为: