'Spring AOP Aspect used in JdbcOperations not invoked
I want to do something like this: http://www.gotoquiz.com/web-coding/programming/java-programming/log-sql-statements-with-parameter-values-filled-in-spring-jdbc/
I have a Spring MVC project and I'm using Spring 4.3.0.RELEASE.
I'm using a Generic Abstract Class for all my DAOS
public abstract class GenericDaoImpl {
@Autowired
protected JdbcTemplate jdbcTemplate;
}
@Repository
public class UserDAOImpl extends GenericDaoImpl implements UserDAO{
}
jdbcTemplate is defined in my auto-config.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
In order to implement something closer to whats in that guide I have:
Added these dependencies to my pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
I've added @EnableAspectJAutoProxy to the @Configuration Class.
And my Aspect is:
@Aspect
@Component
public class SqlParametersLogger {
private final Logger logger = LoggerFactory.getLogger(SqlParametersLogger.class);
@Before("execution(* org.springframework.jdbc.core.JdbcOperations.*(..))")
public void log(JoinPoint jp) {
logger.info("Hello?");
}
}
My Aspect function is never called. I've tried this Aspect with pointcuts of my Service layers, and it works. But with JdbcOperations it does not work.
Any idea?
Solution 1:[1]
Your aop expression is defined as a pointcut to a JdbcOperation.* which is an interface, and interface calls are not advised.
AspectJ can intercept pointcuts in the spring framework for spring-managed-beans. If the bean is not spring managed you'll need to manually register it using ProxyFactoryBean/ProxyFactory for advisal. In your case you already have a bean implementing the JdbcOperations interface registered in your spring application-context.
As per your problem changing the pointcut to use any implementation of the JdbcOperations interface ( the jdbcTemplate bean definition ) should solve your problem.
For example
@Before("execution(* org.springframework.jdbc.core.JdbcOperations+.*(..))")
Solution 2:[2]
you lost 1 .* in the expression,it should be
// this works fine
@Before("execution(* org.springframework.jdbc.core..*JdbcOperations.*(String, ..))")
But you wrote like
// this's not good
@Before("execution(* org.springframework.jdbc.core.JdbcOperations.*(..))")
This answer came to me unexpectedly when I saw this ,link here https://stackoverflow.com/questions/58282255/how-to-intercept-jdbctemplate-whose-instance-is-created-by-myself
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Ciocan |
| Solution 2 | xaiweiyi |
