Aspect for Spring JDBC

梦想的初衷 提交于 2020-01-04 04:40:57

问题


Is that possible to define Spring AOP aspect to Spring JDBC ? To be specific, I am trying to setup a logger for NamedParameterJdbcTemplate to log SQL queries. Below is my XML configuration.

<aop:config>
    <aop:aspect id="aspect4" ref="sqlLoggingInterceptor">
        <aop:pointcut expression="execution(* org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate..*(..))" id="pointcut3" />
        <aop:around pointcut-ref="pointcut3" method="profile" />
    </aop:aspect>
</aop:config>

And below is my interceptor.

@Aspect
@Component
public class SQLLoggingInterceptor {
    private final Map<String, SqlTiming> sqlTimings;

    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    //logger
    }
}

It throws below below exception while creating the context, seems to be not able to autowire proxy in place of NamedParameterJdbcTemplate, where NamedParameterJdbcTemplate is autowired in lot of DAO implementations.

15:06:32,566 INFO  [STDOUT] ERROR [ContextLoader] - Context initialization failed org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name  'com.application.dao.FileStorageDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate com.application.dao.FileStorageDAOImpl.jdbcTemplate; 
nested exception is java.lang.IllegalArgumentException: Can not set org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate field  
com.application.dao.FileStorageDAOImpl.jdbcTemplate to $Proxy166

来源:https://stackoverflow.com/questions/25536282/aspect-for-spring-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!