在Spring中,定义了如下类来处理经过经过验证的Document对象,并且对文档元素及属性进行解析。
public interface ResourceLoader { /** Pseudo URL prefix for loading from the class path: "classpath:" */ String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; /** * Return a Resource handle for the specified resource. * The handle should always be a reusable resource descriptor, * allowing for multiple {@link Resource#getInputStream()} calls. * <p><ul> * <li>Must support fully qualified URLs, e.g. "file:C:/test.dat". * <li>Must support classpath pseudo-URLs, e.g. "classpath:test.dat". * <li>Should support relative file paths, e.g. "WEB-INF/test.dat". * (This will be implementation-specific, typically provided by an * ApplicationContext implementation.) * </ul> * <p>Note that a Resource handle does not imply an existing resource; * you need to invoke {@link Resource#exists} to check for existence. * @param location the resource location * @return a corresponding Resource handle * @see #CLASSPATH_URL_PREFIX * @see org.springframework.core.io.Resource#exists * @see org.springframework.core.io.Resource#getInputStream */ Resource getResource(String location); /** * Expose the ClassLoader used by this ResourceLoader. * <p>Clients which need to access the ClassLoader directly can do so * in a uniform manner with the ResourceLoader, rather than relying * on the thread context ClassLoader. * @return the ClassLoader (never {@code null}) */ ClassLoader getClassLoader(); }
这个接口 定义资源加载器,主要应用于给定的资源文件地址返回对应的Resource(Resource getResource(String location);)。
主要定义资源文件读取并转换为BeanDefinition的各个功能:
public interface BeanDefinitionReader {
/**
* Return the bean factory to register the bean definitions with.
* <p>The factory is exposed through the BeanDefinitionRegistry interface,
* encapsulating the methods that are relevant for bean definition handling.
*/
BeanDefinitionRegistry getRegistry();
/**
* Return the resource loader to use for resource locations.
* Can be checked for the <b>ResourcePatternResolver</b> interface and cast
* accordingly, for loading multiple resources for a given resource pattern.
* <p>Null suggests that absolute resource loading is not available
* for this bean definition reader.
* <p>This is mainly meant to be used for importing further resources
* from within a bean definition resource, for example via the "import"
* tag in XML bean definitions. It is recommended, however, to apply
* such imports relative to the defining resource; only explicit full
* resource locations will trigger absolute resource loading.
* <p>There is also a {@code loadBeanDefinitions(String)} method available,
* for loading bean definitions from a resource location (or location pattern).
* This is a convenience to avoid explicit ResourceLoader handling.
* @see #loadBeanDefinitions(String)
* @see org.springframework.core.io.support.ResourcePatternResolver
*/
ResourceLoader getResourceLoader();
/**
* Return the class loader to use for bean classes.
* <p>{@code null} suggests to not load bean classes eagerly
* but rather to just register bean definitions with class names,
* with the corresponding Classes to be resolved later (or never).
*/
ClassLoader getBeanClassLoader();
/**
* Return the BeanNameGenerator to use for anonymous beans
* (without explicit bean name specified).
*/
BeanNameGenerator getBeanNameGenerator();
/**
* Load bean definitions from the specified resource.
* @param resource the resource descriptor
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException;
/**
* Load bean definitions from the specified resources.
* @param resources the resource descriptors
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException;
/**
* Load bean definitions from the specified resource location.
* <p>The location can also be a location pattern, provided that the
* ResourceLoader of this bean definition reader is a ResourcePatternResolver.
* @param location the resource location, to be loaded with the ResourceLoader
* (or ResourcePatternResolver) of this bean definition reader
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
* @see #getResourceLoader()
* @see #loadBeanDefinitions(org.springframework.core.io.Resource)
* @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
*/
int loadBeanDefinitions(String location) throws BeanDefinitionStoreException;
/**
* Load bean definitions from the specified resource locations.
* @param locations the resource locations, to be loaded with the ResourceLoader
* (or ResourcePatternResolver) of this bean definition reader
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException;
}
里面的主要方法就是loadBeanDefinitions了。
Environment接口本身是一个PropertyResolver,但是提供了Profile特性,即可以根据环境得到相应数据(即激活不同的Profile,可以得到不同的属性数据,比如用于多环境场景的配置(正式机、测试机、开发机DataSource配置))
public interface EnvironmentCapable { /** * Return the {@link Environment} associated with this component. */ Environment getEnvironment(); }
对EnvironmentCapable,BeanDefinitionReader类定义的功能进行实现:包括loadBeanDefinitions和getEnvironment。
public interface BeanDefinitionDocumentReader { /** * Set the Environment to use when reading bean definitions. * <p>Used for evaluating profile information to determine whether a * {@code <beans/>} document/element should be included or ignored. */ void setEnvironment(Environment environment); /** * Read bean definitions from the given DOM document and * register them with the registry in the given reader context. * @param doc the DOM document * @param readerContext the current context of the reader * (includes the target registry and the resource being parsed) * @throws BeanDefinitionStoreException in case of parsing errors */ void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) throws BeanDefinitionStoreException; }
public interface DocumentLoader { /** * Load a {@link Document document} from the supplied {@link InputSource source}. * @param inputSource the source of the document that is to be loaded * @param entityResolver the resolver that is to be used to resolve any entities * @param errorHandler used to report any errors during document loading * @param validationMode the type of validation * {@link org.springframework.util.xml.XmlValidationModeDetector#VALIDATION_DTD DTD} * or {@link org.springframework.util.xml.XmlValidationModeDetector#VALIDATION_XSD XSD}) * @param namespaceAware {@code true} if support for XML namespaces is to be provided * @return the loaded {@link Document document} * @throws Exception if an error occurs */ Document loadDocument( InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception; }
定义了加载XML配置的方法,这里的XML文件是InputSource类型。
定义解析Element的各种方法:
发现大神写一段逻辑,都是一个接口,一个抽象类,然后默认实现类。。。或者更复杂的结构。都是只提供默认实现(Default**Class),如需改动,自行安装接口扩展。
后续博客会继续跟进bean解析过程,马上就到解析标签,属性,并注册的过程啦。