我正在尝试使用面向方面的编程在 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 本身不会,即使您希望它这样做,可能是因为该接口(interface)实际上是 deprecated since Spring 3.1 .
顺便说一句,只是为了好玩,如果您不介意还捕获 NamedParameterJdbcOperations
,我有一个关于如何实际缩短切入点的想法:
@Before("execution(* org.springframework.jdbc.core..*JdbcOperations.*(String, ..))")
关于java - 通过 AOP 从 JdbcTemplate 或 SimpleJdbcTemplate 记录 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158016/