我发现这是目前不可能的;OGNL 实际上无法访问配置属性。
作为一种解决方法,正如mybatis 邮件列表上的这篇文章中所建议的那样,我编写了一个简单的拦截器,它读取配置参数并将它们添加到查询参数映射中。不完全干净,但它有效。
拦截器代码:
@Intercepts({
@Signature(type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class ConfigPropInterceptor implements Interceptor {
private final Map<String, Object> properties = new HashMap<String, Object>();
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object param = invocation.getArgs()[1];
if (param instanceof Map) {
((Map<String, Object>)param).putAll(properties);
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
for (String p : properties.stringPropertyNames()) {
this.properties.put(p, properties.getProperty(p));
}
}
}
配置 .xml 中的示例用法:
<plugins>
<plugin interceptor="...ConfigPropInterceptor">
<property name="_issuerLocation" value="${issuer.location}"/>
</plugin>
</plugins>
使用此设置,我能够_issuerLocation
像其他所有内容一样测试 OGNL 表达式中的变量。