我發現這是目前不可能的; OGNL實際上無法訪問配置屬性。
作爲mybatis郵件列表中的this post建議的解決方法,我編寫了一個簡單的攔截器,它讀取配置參數並將它們添加到查詢參數映射中。不完全乾淨,但它的工作原理。
攔截代碼:
@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表達式等一切。