public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
//委托XMLConfigBuilder来解析xml文件,并构建Configuration,
//根据Xnode.evalNode。这个builder里具体解析xml挺多的
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT"/>
<property name="username" value=""/>
<property name="password" value=""/>
</dataSource>
</environment>
environmentsElement(root.evalNode("environments"));
//xml解析里比较重要的
//环境解析
//7.1事务管理器
//根据type="JDBC"解析返回适当的TransactionFactory
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
//7.2数据源
//根据type="POOLED"解析返回适当的DataSourceFactory
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
DataSource dataSource = dsFactory.getDataSource();
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
configuration.setEnvironment(environmentBuilder.build());
返回factory的这几个方法都调用了resolveClass, 根据别名解析Class,其实是去查看 类型别名注册/事务管理器别名
public void registerAlias(String alias, Class<?> value) {
if (alias == null) {
throw new TypeException("The parameter alias cannot be null");
}
String key = alias.toLowerCase(Locale.ENGLISH);
if (TYPE_ALIASES.containsKey(key) && TYPE_ALIASES.get(key) != null && !TYPE_ALIASES.get(key).equals(value)) {
throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + TYPE_ALIASES.get(key).getName() + "'.");
}
TYPE_ALIASES.put(key, value);
}
这个方法应该也是之前用过的标签所调用的,盲猜会将目标类放在TYPE_ALIASES这张表里。
最后一个build方法使用了一个Configuration作为参数,并返回DefaultSqlSessionFactory。这个default实现了SqlSessionFactory接口,
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
来源:CSDN
作者:xunhan0991
链接:https://blog.csdn.net/jx515519290/article/details/103645913