Aspect for Spring JDBC

2019-07-02 01:22发布

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

0条回答
登录 后发表回答