通过 AOP 从 JdbcTemplate 或 SimpleJdbcTemplate 记录 SQL 语句
Log SQL Statements from JdbcTemplate or SimpleJdbcTemplate via AOP
我正在尝试使用面向方面的编程在 spring JdbcTemplate 和 SimpleJdbcTemplate 中记录执行的 SQL 查询。我从这个 tutorial
中复制粘贴了代码
切入点定义为
@Before(
"execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..))"
)
非常适合拦截 JdbcTemplate 查询。但是当我将切入点更改为
@Before(
"execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..)) throws *Exception || " +
"execution(* org.springframework.jdbc.core.simple.SimpleJdbcOperations.*(String, ..)) throws *Exception"
)
或
@Before(
"execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..)) || " +
"execution(* org.springframework.jdbc.core.simple.SimpleJdbcOperations.*(String, ..))"
)
然后只有 JdbcTemplate 查询被拦截,没有 SimpleJdbcTemplate
查询。
任何提示如何通过 AOP 拦截来自 SimpleJdbcTemplate 的查询同时保留来自 JdbcTemplate 的日志查询?
作为 AspectJ 用户,我可以说您的切入点看起来不错。作为非 Spring 用户,我只能推测
- 也许您的代码根本不使用
SimpleJdbcOperations
或
- 也许 Spring 本身并没有,即使你期望它,可能是因为接口实际上是 deprecated since Spring 3.1.
顺便说一句,如果您不介意同时捕获 NamedParameterJdbcOperations
:
,我有一个想法可以告诉您如何实际缩短切入点
@Before("execution(* org.springframework.jdbc.core..*JdbcOperations.*(String, ..))")
我正在尝试使用面向方面的编程在 spring JdbcTemplate 和 SimpleJdbcTemplate 中记录执行的 SQL 查询。我从这个 tutorial
中复制粘贴了代码切入点定义为
@Before(
"execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..))"
)
非常适合拦截 JdbcTemplate 查询。但是当我将切入点更改为
@Before(
"execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..)) throws *Exception || " +
"execution(* org.springframework.jdbc.core.simple.SimpleJdbcOperations.*(String, ..)) throws *Exception"
)
或
@Before(
"execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..)) || " +
"execution(* org.springframework.jdbc.core.simple.SimpleJdbcOperations.*(String, ..))"
)
然后只有 JdbcTemplate 查询被拦截,没有 SimpleJdbcTemplate 查询。
任何提示如何通过 AOP 拦截来自 SimpleJdbcTemplate 的查询同时保留来自 JdbcTemplate 的日志查询?
作为 AspectJ 用户,我可以说您的切入点看起来不错。作为非 Spring 用户,我只能推测
- 也许您的代码根本不使用
SimpleJdbcOperations
或 - 也许 Spring 本身并没有,即使你期望它,可能是因为接口实际上是 deprecated since Spring 3.1.
顺便说一句,如果您不介意同时捕获 NamedParameterJdbcOperations
:
@Before("execution(* org.springframework.jdbc.core..*JdbcOperations.*(String, ..))")