java 拦截所有SQL查询的方面

8yoxcaq7  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(157)

我有一个带有方面的库,它可以扫描所有语句的执行,并使用SQL语句进行一些逻辑处理。

@Pointcut("target(java.sql.Statement)")
public void statement() {}

@AfterReturning("!within(AnalyzerAspect) && statement() && args(sql)")
public void after(JoinPoint jp, String sql) throws Throwable {
  // some logic
}

当我使用它进行jdbc操作时,它工作得很好。我还使用了带有aspectj插件选项的spring-jdbc库

<plugin>
                <groupId>dev.aspectj</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>${aspect.compiler.plugin.version}</version>
                <configuration>
                    <weaveDependencies>
                        <weaveDependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-jdbc</artifactId>
                        </weaveDependency>
                        <weaveDependency>
                            <groupId>org.hibernate</groupId> 
                            <artifactId>hibernate-core</artifactId>
                        </weaveDependency>
                    </weaveDependencies>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>edu.ifmo.diploma</groupId> <!-- My library -->
                            <artifactId>db-analyzer</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <complianceLevel>${maven.compiler.target}</complianceLevel>
                    <showWeaveInfo>true</showWeaveInfo>
                    <Xlint>ignore</Xlint>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

和查询执行

jdbcTemplate.query("SELECT * FROM person", new BeanPropertyRowMapper<>(Person.class));

和方面工作正常,但当我试图使用它与spring-data-jpa存储库

public interface PersonRepository extends JpaRepository<Person, UUID> {

    @Query(nativeQuery = true, value = "SELECT * FROM person")
    List<Person> findAll();
}
personRepository.findAll();

我的方面不拦截它的执行,尽管在我的插件中编织了hibernate。
我的主要目标是改进这个库,使它不依赖于db库,只拦截jdbc语句的执行,因为所有的db库都在后台使用它。如果你给我的建议比我的更简单,我会感激你的。

pqwbnv8z

pqwbnv8z1#

我忘记了在我的方面检查准备好的语句,我调试了我的存储库调用并检测到它调用了preparedStatement.executeQuery()。
现在我的方面拦截语句和准备语句执行。

@Pointcut("target(java.sql.Statement)")
    fun statement() {
    }

    @Pointcut("target(java.sql.PreparedStatement)")
    fun preparedStatement() {
    }

    @AfterReturning("!within(AnalyzerAspect) && preparedStatement()")
    fun afterWithoutSqlArgs(jp: JoinPoint) {
        if (!jp.signature.name.startsWith("execute")) {
            return
        }
        val target = jp.target as PreparedStatement
        after(jp, target.toString())
    }

    @AfterReturning("!within(AnalyzerAspect) && statement() && args(sql)")
    fun after(jp: JoinPoint, sql: String) {
        //some logic
    }

如果你有相同的任务拦截所有的SQL查询,你必须把方面以上在您的代码和配置aspectj插件这种方式

<plugin>
                <groupId>dev.aspectj</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>${aspect.compiler.plugin.version}</version>
                <configuration>
                    <weaveDependencies>
                        <weaveDependency>
                            <groupId>org.postgresql</groupId> <!-- or another driver you use -->
                            <artifactId>postgresql</artifactId>
                        </weaveDependency>
                    </weaveDependencies>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>edu.ifmo.diploma</groupId> <!-- library with aspect -->
                            <artifactId>db-analyzer</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <complianceLevel>${maven.compiler.target}</complianceLevel>
                    <showWeaveInfo>true</showWeaveInfo>
                    <Xlint>ignore</Xlint>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

相关问题