@ -0,0 +1,112 @@
|
||||
# Mybatis MetaObject
|
||||
- Author: [HuiFer](https://github.com/huifer)
|
||||
- 源码阅读工程: [huifer-mybatis](https://github.com/huifer/javaBook-src/tree/old/mybatis-3)
|
||||
- 源码位于:`org.apache.ibatis.reflection.MetaObject`
|
||||
```java
|
||||
/**
|
||||
* @author Clinton Begin
|
||||
*/
|
||||
public class MetaObject {
|
||||
|
||||
/**
|
||||
* 原始的数据对象,初始化时的对象
|
||||
*/
|
||||
private final Object originalObject;
|
||||
/**
|
||||
* 对象包装
|
||||
*/
|
||||
private final ObjectWrapper objectWrapper;
|
||||
/**
|
||||
* object 工厂
|
||||
*/
|
||||
private final ObjectFactory objectFactory;
|
||||
/**
|
||||
* object
|
||||
*/
|
||||
private final ObjectWrapperFactory objectWrapperFactory;
|
||||
/**
|
||||
* 反射工程
|
||||
*/
|
||||
private final ReflectorFactory reflectorFactory;
|
||||
|
||||
private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
|
||||
this.originalObject = object;
|
||||
this.objectFactory = objectFactory;
|
||||
this.objectWrapperFactory = objectWrapperFactory;
|
||||
this.reflectorFactory = reflectorFactory;
|
||||
|
||||
// 根据object不同实例进行不同的实例化方式
|
||||
if (object instanceof ObjectWrapper) {
|
||||
this.objectWrapper = (ObjectWrapper) object;
|
||||
} else if (objectWrapperFactory.hasWrapperFor(object)) {
|
||||
this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
|
||||
} else if (object instanceof Map) {
|
||||
this.objectWrapper = new MapWrapper(this, (Map) object);
|
||||
} else if (object instanceof Collection) {
|
||||
this.objectWrapper = new CollectionWrapper(this, (Collection) object);
|
||||
} else {
|
||||
this.objectWrapper = new BeanWrapper(this, object);
|
||||
}
|
||||
}
|
||||
|
||||
public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
|
||||
if (object == null) {
|
||||
return SystemMetaObject.NULL_META_OBJECT;
|
||||
} else {
|
||||
return new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取value
|
||||
* @param name 属性值名称
|
||||
* @return
|
||||
*/
|
||||
public Object getValue(String name) {
|
||||
PropertyTokenizer prop = new PropertyTokenizer(name);
|
||||
if (prop.hasNext()) {
|
||||
MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
|
||||
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
|
||||
// 判断是否是空的metaObject
|
||||
return null;
|
||||
} else {
|
||||
return metaValue.getValue(prop.getChildren());
|
||||
}
|
||||
} else {
|
||||
return objectWrapper.get(prop);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* metaObject 设置属性值方法
|
||||
* {name:value}
|
||||
*
|
||||
* @param name 属性值名称
|
||||
* @param value 属性值
|
||||
*/
|
||||
public void setValue(String name, Object value) {
|
||||
PropertyTokenizer prop = new PropertyTokenizer(name);
|
||||
if (prop.hasNext()) {
|
||||
// 获取属性实例
|
||||
MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
|
||||
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
|
||||
if (value == null) {
|
||||
// value 空则返回
|
||||
// don't instantiate child path if value is null
|
||||
return;
|
||||
} else {
|
||||
// 创建属性值
|
||||
metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
|
||||
}
|
||||
}
|
||||
|
||||
metaValue.setValue(prop.getChildren(), value);
|
||||
} else {
|
||||
objectWrapper.set(prop, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,158 @@
|
||||
# EntityResolver
|
||||
- Author: [HuiFer](https://github.com/huifer)
|
||||
- 源码阅读仓库: [huifer-spring](https://github.com/huifer/spring-framework)
|
||||
- 源码路径: `org.xml.sax.EntityResolver`,非Spring类
|
||||
|
||||
## DelegatingEntityResolver#resolveEntity
|
||||
- org.springframework.beans.factory.xml.DelegatingEntityResolver.resolveEntity
|
||||
```java
|
||||
@Override
|
||||
@Nullable
|
||||
public InputSource resolveEntity(@Nullable String publicId, @Nullable String systemId)
|
||||
throws SAXException, IOException {
|
||||
|
||||
if (systemId != null) {
|
||||
if (systemId.endsWith(DTD_SUFFIX)) {
|
||||
return this.dtdResolver.resolveEntity(publicId, systemId);
|
||||
}
|
||||
else if (systemId.endsWith(XSD_SUFFIX)) {
|
||||
return this.schemaResolver.resolveEntity(publicId, systemId);
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the parser's default behavior.
|
||||
return null;
|
||||
}
|
||||
|
||||
```
|
||||
- 上述这段代码是针对xml进行校验
|
||||
```xml
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
```
|
||||
- 如上所示以`.xsd`结尾,应该执行` return this.schemaResolver.resolveEntity(publicId, systemId);`方法
|
||||
`http://www.springframework.org/schema/beans/spring-beans.xsd`
|
||||
- `org.springframework.beans.factory.xml.PluggableSchemaResolver.resolveEntity`
|
||||
## PluggableSchemaResolver#resolveEntity
|
||||
```java
|
||||
@Override
|
||||
@Nullable
|
||||
public InputSource resolveEntity(@Nullable String publicId, @Nullable String systemId) throws IOException {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Trying to resolve XML entity with public id [" + publicId +
|
||||
"] and system id [" + systemId + "]");
|
||||
}
|
||||
|
||||
if (systemId != null) {
|
||||
// 获取当前 systemId 对应的资源
|
||||
// spring-framework\spring-beans\src\main\resources\org\springframework\beans\factory\xml\spring-beans.xsd
|
||||
String resourceLocation = getSchemaMappings().get(systemId);
|
||||
if (resourceLocation == null && systemId.startsWith("https:")) {
|
||||
// Retrieve canonical http schema mapping even for https declaration
|
||||
resourceLocation = getSchemaMappings().get("http:" + systemId.substring(6));
|
||||
}
|
||||
if (resourceLocation != null) {
|
||||
// 加载 resourceLocation 转换成 Resource
|
||||
Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
|
||||
try {
|
||||
// 读取
|
||||
InputSource source = new InputSource(resource.getInputStream());
|
||||
source.setPublicId(publicId);
|
||||
source.setSystemId(systemId);
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
catch (FileNotFoundException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not find XML schema [" + systemId + "]: " + resource, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the parser's default behavior.
|
||||
return null;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
![image-20200108081404857](/images/spring//image-20200108081404857.png)
|
||||
|
||||
![image-20200108081623427](/images/spring//image-20200108081623427.png)
|
||||
|
||||
得到本地路径,后续直接返回读取资源
|
||||
|
||||
## BeansDtdResolver#resolveEntity
|
||||
|
||||
创建一个Dtd的约束文件
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
</beans>
|
||||
```
|
||||
|
||||
```java
|
||||
@Override
|
||||
@Nullable
|
||||
public InputSource resolveEntity(@Nullable String publicId, @Nullable String systemId) throws IOException {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Trying to resolve XML entity with public ID [" + publicId +
|
||||
"] and system ID [" + systemId + "]");
|
||||
}
|
||||
|
||||
if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
|
||||
int lastPathSeparator = systemId.lastIndexOf('/');
|
||||
int dtdNameStart = systemId.indexOf(DTD_NAME, lastPathSeparator);
|
||||
if (dtdNameStart != -1) {
|
||||
// 获取静态变量组装成文件名称(spring-beans.dtd)
|
||||
String dtdFile = DTD_NAME + DTD_EXTENSION;
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Trying to locate [" + dtdFile + "] in Spring jar on classpath");
|
||||
}
|
||||
try {
|
||||
// 加载资源
|
||||
Resource resource = new ClassPathResource(dtdFile, getClass());
|
||||
InputSource source = new InputSource(resource.getInputStream());
|
||||
source.setPublicId(publicId);
|
||||
source.setSystemId(systemId);
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
catch (FileNotFoundException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the parser's default behavior.
|
||||
return null;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
- systemId `https://www.springframework.org/dtd/spring-beans-2.0.dtd`
|
||||
|
||||
![image-20200108082335031](/images/spring//image-20200108082335031.png)
|
||||
|
||||
|
||||
## 总结
|
||||
|
||||
- DelegatingEntityResolver#resolveEntity,是对xml文档的校验前置步骤,根据`dtd`和`xsd`加载本地资源文件
|
||||
`spring-framework\spring-beans\src\main\resources\org\springframework\beans\factory\xml\spring-beans.dtd`
|
||||
`spring-framework\spring-beans\src\main\resources\org\springframework\beans\factory\xml\spring-beans.xsd`
|
||||
- `PluggableSchemaResolver`负责加载`xsd`文件
|
||||
- `BeansDtdResolver`负责加载`dtd`文件
|
@ -0,0 +1,307 @@
|
||||
# Spring-SimpleAliasRegistry
|
||||
- Author: [HuiFer](https://github.com/huifer)
|
||||
- 源码阅读仓库: [huifer-spring](https://github.com/huifer/spring-framework)
|
||||
|
||||
## AliasRegistry
|
||||
- `SimpleAliasRegistry`继承`org.springframework.core.AliasRegistry`
|
||||
```java
|
||||
public interface AliasRegistry {
|
||||
|
||||
/**
|
||||
* Given a name, register an alias for it.
|
||||
* 别名注册
|
||||
*
|
||||
* @param name the canonical name
|
||||
* @param alias the alias to be registered
|
||||
* @throws IllegalStateException if the alias is already in use
|
||||
* and may not be overridden
|
||||
* @see SimpleAliasRegistry
|
||||
* @see org.springframework.context.support.GenericApplicationContext
|
||||
*/
|
||||
void registerAlias(String name, String alias);
|
||||
|
||||
/**
|
||||
* Remove the specified alias from this registry.
|
||||
* 别名移除
|
||||
*
|
||||
* @param alias the alias to remove
|
||||
* @throws IllegalStateException if no such alias was found
|
||||
*/
|
||||
void removeAlias(String alias);
|
||||
|
||||
/**
|
||||
* Determine whether this given name is defines as an alias
|
||||
* (as opposed to the name of an actually registered component).
|
||||
* 是不是别名
|
||||
*
|
||||
* @param name the name to check
|
||||
* @return whether the given name is an alias
|
||||
*/
|
||||
boolean isAlias(String name);
|
||||
|
||||
/**
|
||||
* Return the aliases for the given name, if defined.
|
||||
* 从别名注册map中获取别名信息
|
||||
*
|
||||
* @param name the name to check for aliases
|
||||
* @return the aliases, or an empty array if none
|
||||
*/
|
||||
String[] getAliases(String name);
|
||||
|
||||
}
|
||||
```
|
||||
## SimpleAliasRegistry
|
||||
```java
|
||||
/**
|
||||
* Simple implementation of the {@link AliasRegistry} interface.
|
||||
* Serves as base class for
|
||||
* {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}
|
||||
* implementations.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public class SimpleAliasRegistry implements AliasRegistry {
|
||||
|
||||
/**
|
||||
* Logger available to subclasses.
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
/**
|
||||
* Map from alias to canonical name.
|
||||
* 存放别名的map(线程安全的),
|
||||
* 结构: alias-> name
|
||||
*/
|
||||
private final Map<String, String> aliasMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
|
||||
/**
|
||||
* {@code <alias name="appleBean" alias="hhh"/>}
|
||||
*
|
||||
* @param name the canonical name
|
||||
* alias 标签的name属性
|
||||
* @param alias the alias to be registered
|
||||
* alias 标签的alias属性
|
||||
*/
|
||||
@Override
|
||||
public void registerAlias(String name, String alias) {
|
||||
Assert.hasText(name, "'name' must not be empty");
|
||||
Assert.hasText(alias, "'alias' must not be empty");
|
||||
synchronized (this.aliasMap) {
|
||||
// 判断: 别名和名字是否相同
|
||||
if (alias.equals(name)) {
|
||||
//相同在别名map中移除
|
||||
this.aliasMap.remove(alias);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Alias definition '" + alias + "' ignored since it points to same name");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 不相同
|
||||
// 从map对象中获取别名为alias的value
|
||||
String registeredName = this.aliasMap.get(alias);
|
||||
if (registeredName != null) {
|
||||
// 判断map中是否有有一个别名和传入的name相同的内容
|
||||
if (registeredName.equals(name)) {
|
||||
// An existing alias - no need to re-register
|
||||
return;
|
||||
}
|
||||
if (!allowAliasOverriding()) {
|
||||
throw new IllegalStateException("Cannot define alias '" + alias + "' for name '" +
|
||||
name + "': It is already registered for name '" + registeredName + "'.");
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Overriding alias '" + alias + "' definition for registered name '" +
|
||||
registeredName + "' with new target name '" + name + "'");
|
||||
}
|
||||
}
|
||||
// 别名环检查
|
||||
checkForAliasCircle(name, alias);
|
||||
// 放入 map 对象中 alias-> name
|
||||
this.aliasMap.put(alias, name);
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Alias definition '" + alias + "' registered for name '" + name + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether alias overriding is allowed.
|
||||
* Default is {@code true}.
|
||||
* 是否允许重写别名
|
||||
*/
|
||||
protected boolean allowAliasOverriding() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given name has the given alias registered.
|
||||
* <p>
|
||||
* 递归判断是否已经存在别名
|
||||
*
|
||||
* @param name the name to check
|
||||
* @param alias the alias to look for
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public boolean hasAlias(String name, String alias) {
|
||||
for (Map.Entry<String, String> entry : this.aliasMap.entrySet()) {
|
||||
// 获取key值
|
||||
String registeredName = entry.getValue();
|
||||
if (registeredName.equals(name)) {
|
||||
String registeredAlias = entry.getKey();
|
||||
// 循环引用判断
|
||||
if (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 别名移除
|
||||
*
|
||||
* @param alias the alias to remove
|
||||
*/
|
||||
@Override
|
||||
public void removeAlias(String alias) {
|
||||
synchronized (this.aliasMap) {
|
||||
// 判断是否移除成功
|
||||
String name = this.aliasMap.remove(alias);
|
||||
if (name == null) {
|
||||
throw new IllegalStateException("No alias '" + alias + "' registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是一个别名,校验方式{@link org.springframework.core.SimpleAliasRegistry#aliasMap} 的key是否包含
|
||||
*
|
||||
* @param name the name to check
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean isAlias(String name) {
|
||||
return this.aliasMap.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取别名列表
|
||||
*
|
||||
* @param name the name to check for aliases
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String[] getAliases(String name) {
|
||||
List<String> result = new ArrayList<>();
|
||||
synchronized (this.aliasMap) {
|
||||
retrieveAliases(name, result);
|
||||
}
|
||||
return StringUtils.toStringArray(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitively retrieve all aliases for the given name.
|
||||
* 根据 name 获取别名
|
||||
*
|
||||
* @param name the target name to find aliases for
|
||||
* @param result the resulting aliases list
|
||||
*/
|
||||
private void retrieveAliases(String name, List<String> result) {
|
||||
// 循环获取
|
||||
this.aliasMap.forEach((alias, registeredName) -> {
|
||||
if (registeredName.equals(name)) {
|
||||
result.add(alias);
|
||||
// 递归查询循环引用的别名
|
||||
retrieveAliases(alias, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve all alias target names and aliases registered in this
|
||||
* factory, applying the given StringValueResolver to them.
|
||||
* <p>The value resolver may for example resolve placeholders
|
||||
* in target bean names and even in alias names.
|
||||
*
|
||||
* @param valueResolver the StringValueResolver to apply
|
||||
*/
|
||||
public void resolveAliases(StringValueResolver valueResolver) {
|
||||
Assert.notNull(valueResolver, "StringValueResolver must not be null");
|
||||
synchronized (this.aliasMap) {
|
||||
Map<String, String> aliasCopy = new HashMap<>(this.aliasMap);
|
||||
aliasCopy.forEach((alias, registeredName) -> {
|
||||
String resolvedAlias = valueResolver.resolveStringValue(alias);
|
||||
String resolvedName = valueResolver.resolveStringValue(registeredName);
|
||||
if (resolvedAlias == null || resolvedName == null || resolvedAlias.equals(resolvedName)) {
|
||||
this.aliasMap.remove(alias);
|
||||
}
|
||||
else if (!resolvedAlias.equals(alias)) {
|
||||
String existingName = this.aliasMap.get(resolvedAlias);
|
||||
if (existingName != null) {
|
||||
if (existingName.equals(resolvedName)) {
|
||||
// Pointing to existing alias - just remove placeholder
|
||||
this.aliasMap.remove(alias);
|
||||
return;
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"Cannot register resolved alias '" + resolvedAlias + "' (original: '" + alias +
|
||||
"') for name '" + resolvedName + "': It is already registered for name '" +
|
||||
registeredName + "'.");
|
||||
}
|
||||
checkForAliasCircle(resolvedName, resolvedAlias);
|
||||
this.aliasMap.remove(alias);
|
||||
this.aliasMap.put(resolvedAlias, resolvedName);
|
||||
}
|
||||
else if (!registeredName.equals(resolvedName)) {
|
||||
this.aliasMap.put(alias, resolvedName);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given name points back to the given alias as an alias
|
||||
* in the other direction already, catching a circular reference upfront
|
||||
* and throwing a corresponding IllegalStateException.
|
||||
* <p>
|
||||
* 判断是否循环别名
|
||||
*
|
||||
* @param name the candidate name
|
||||
* @param alias the candidate alias
|
||||
* @see #registerAlias
|
||||
* @see #hasAlias
|
||||
*/
|
||||
protected void checkForAliasCircle(String name, String alias) {
|
||||
if (hasAlias(alias, name)) {
|
||||
throw new IllegalStateException("Cannot register alias '" + alias +
|
||||
"' for name '" + name + "': Circular reference - '" +
|
||||
name + "' is a direct or indirect alias for '" + alias + "' already");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the raw name, resolving aliases to canonical names.
|
||||
*
|
||||
* @param name the user-specified name
|
||||
* @return the transformed name
|
||||
*/
|
||||
public String canonicalName(String name) {
|
||||
String canonicalName = name;
|
||||
// Handle aliasing...
|
||||
String resolvedName;
|
||||
do {
|
||||
resolvedName = this.aliasMap.get(canonicalName);
|
||||
if (resolvedName != null) {
|
||||
canonicalName = resolvedName;
|
||||
}
|
||||
}
|
||||
while (resolvedName != null);
|
||||
return canonicalName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 86 KiB |
After Width: | Height: | Size: 76 KiB |
After Width: | Height: | Size: 72 KiB |
After Width: | Height: | Size: 75 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 61 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 44 KiB |
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 26 KiB |