2

我有一个使用 apache dbcp 和 spring jdbc 在 oracle 数据库上执行数据库操作的 Web 应用程序。我需要编写一个性能记录器来记录每个数据库操作的各个时间。我尝试为 org.springframework.jdbc.core.JdbcTemplate 的所有“执行”方法编写一个环绕建议,但是在 spring 初始化时会导致错误。记录器类和异常堆栈跟踪如下:-

我还尝试通过启用来使用 CGLIB 代理,但它在从 spring 的 StoredProcedure 类扩展的 dao 类上出错并使用构造函数注入。

@Aspect
public class Logger {

@Around("this(org.springframework.jdbc.core.JdbcTemplate) && execution(* execute(*))")
public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
    long time = System.currentTimeMillis();
    Object result = pjp.proceed();
    LOGGER.debug("time consumed = " + (System.currentTimeMillis() - time));
    return result;
}

异常堆栈跟踪:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'myDao' defined in class path resource [spring/my-dao.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.jdbc.core.JdbcTemplate]:
Could not convert constructor argument value of type [$Proxy7] to required type [org.springframework.jdbc.core.JdbcTemplate]:
Failed to convert value of type '$Proxy7 implementing org.springframework.jdbc.core.JdbcOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'org.springframework.jdbc.core.JdbcTemplate';
nested exception is
java.lang.IllegalStateException: Cannot convert value of type [$Proxy7 implementing org.springframework.jdbc.core.JdbcOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [org.springframework.jdbc.core.JdbcTemplate]:
no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:702)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)
4

0 回答 0