旧的 spring XML 配置工作正常,我的 Aspect 调用每个人 getconnection Invoke :
<bean id="connectionInterceptor" class="mypackage.ConnectionAspect">
</bean>
<aop:config>
<aop:aspect id="connectionLifecycleAspect" ref="connectionInterceptor">
<aop:pointcut
expression="execution(java.sql.Connection javax.sql.DataSource.getConnection(..)) "
id="dataSourceGetConnection" />
<aop:around method="aroundGetConnection" pointcut-ref="dataSourceGetConnection" />
</aop:aspect>
</aop:config>
我的java类围绕获取连接
public class ConnectionAspect implements Serializable {
private final static Logger logger = Logger.getLogger(ConnectionAspect.class);
public Connection aroundGetConnection(ProceedingJoinPoint joinPoint) throws Throwable {
Connection connection = (Connection) joinPoint.proceed();
setSessionUser(connection);
return connection;
}
Spring boot 不起作用,当我调用 get Connection 时没有调用任何方法。 但是 spring 服务 aop 演示正在工作。
@Component
@Aspect
public class ConnectionAspect implements Serializable {
@AfterReturning(pointcut = "execution(* javax.sql.DataSource.getConnection(..))", returning = "connection")
public Connection setClientIdentifier(Connection connection)
throws SQLException {
System.out
.println("-----------------------------------------------------------------------");
return connection;
}
@Pointcut("execution(java.sql.Connection javax.sql.DataSource.getConnection(..))")
public void aroundConnection() {
}
@Around("aroundConnection()")
public Object aroundConnection2(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Completed: " + joinPoint);
return joinPoint.proceed();
}
@Around("execution(java.sql.Connection javax.sql.DataSource.getConnection(..))")
public Object aroundConnection3(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("Completed: " + joinPoint);
return joinPoint.proceed();
}
}
最佳答案
SpringBoot默认使用tomcat-jdbc作为DataSource实现。那么下面的代码就可以正常工作了。
@AfterReturning(pointcut = "execution(* org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(..))", returning = "connection")
public Connection setClientIdentifier(Connection connection) throws SQLException {
System.out.println("-----------------------");
return connection;
}
关于Spring boot aop 围绕 Datasource.getConnection 不起作用,有人知道吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163314/