RoutingStatementHandler statement = (RoutingStatementHandler)invocation.getTarget();
PreparedStatementHandler handler = (PreparedStatementHandler)ReflectUtil.getFieldValue(statement,
"delegate");
RowBounds rowBounds = (RowBounds)ReflectUtil.getFieldValue(handler,
"rowBounds");
if(rowBounds!=null)
{
if (rowBounds.getLimit() > 0
&& rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT)
{
BoundSql boundSql = statement.getBoundSql();
String sql = boundSql.getSql();
Dialect dialect = (Dialect)Class.forName(DIALECT).newInstance();
sql = dialect.getLimitString(sql,
rowBounds.getOffset(),
rowBounds.getLimit());
ReflectUtil.setFieldValue(boundSql, "sql", sql);
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties arg0) {
}
}
@Intercepts( {@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class StatementHandlerInterceptor implements Interceptor {
private String DIALECT ;
public String getDIALECT() {
return DIALECT;
}
public void setDIALECT(String dIALECT) {
DIALECT = dIALECT;
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler statement = (RoutingStatementHandler)invocation.getTarget();
PreparedStatementHandler handler = (PreparedStatementHandler)ReflectUtil.getFieldValue(statement,
"delegate");
RowBounds rowBounds = (RowBounds)ReflectUtil.getFieldValue(handler,
"rowBounds");
if(rowBounds!=null)
{
if (rowBounds.getLimit() > 0
&& rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT)
{
BoundSql boundSql = statement.getBoundSql();
String sql = boundSql.getSql();
Dialect dialect = (Dialect)Class.forName(DIALECT).newInstance();
sql = dialect.getLimitString(sql,
rowBounds.getOffset(),
rowBounds.getLimit());
ReflectUtil.setFieldValue(boundSql, "sql", sql);
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties arg0) {
}
}
上面那段代码是我实现的一个拦截StatementHandler的prepare方法的插件。看到我在配置拦截目标的时候用到了这样一个注解:
@Intercepts( {@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
这里面有个@Signature注解,这个单词是否和我上面讲到的一个方法名中包含这个单词。对,就是getSignatureMap这个方法。可以很容易想到这个方法就是处理这个注解的。接下来展开看一下getSignatureMap方法所要执行的操作。
[python] private static Map
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
if (interceptsAnnotation == null) { // issue #251
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = in