Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图拦截JDBC操作并返回带有字节的固定值_Java_Spring_Instrumentation_Byte Buddy - Fatal编程技术网

Java 试图拦截JDBC操作并返回带有字节的固定值

Java 试图拦截JDBC操作并返回带有字节的固定值,java,spring,instrumentation,byte-buddy,Java,Spring,Instrumentation,Byte Buddy,我试图使用byte-buddy和spring截获对jdbc操作的调用 我有两节课 Application.java @SpringBootApplication public class Application { public static void main(String[] args) throws Exception{ premain(null, ByteBuddyAgent.install()); SpringApplicati

我试图使用byte-buddy和spring截获对jdbc操作的调用

我有两节课

Application.java

    @SpringBootApplication
    public class Application {

    public static void main(String[] args) throws Exception{
        premain(null, ByteBuddyAgent.install());
        SpringApplication.run(Application.class, args);
    }


    public static void premain(String arg, Instrumentation instrumentation) throws Exception {
        new AgentBuilder.Default()
                .type(ElementMatchers.is(JdbcOperations.class))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
                        return builder.method(named("queryForList"))
                                .intercept(FixedValue.value(Collections.emptyList()));
                    }
                }).installOn(instrumentation);
    }
} 
@springboot应用程序
公共类应用程序{
公共静态void main(字符串[]args)引发异常{
premain(null,ByteBuddyAgent.install());
SpringApplication.run(Application.class,args);
}
公共静态void premain(字符串arg,检测)引发异常{
新建AgentBuilder.Default()
.type(ElementMatchers.is(JdbcOperations.class))
.transform(新的AgentBuilder.Transformer(){
@凌驾
公共DynamicType.Builder转换(DynamicType.Builder、TypeDescription、TypeDescription、ClassLoader、ClassLoader、JavaModule、JavaModule){
返回builder.method(名为(“queryForList”))
.intercept(FixedValue.value(Collections.emptyList());
}
}).installOn(仪器仪表);
}
} 

DemoRunner.java

    @Component
    public class DemoRunner implements CommandLineRunner {
    private final JdbcOperations jdbcOperations;

    public DemoRunner(JdbcOperations jdbcOperations) {
        this.jdbcOperations = jdbcOperations;
    }


    @Override
    public void run(String... args) throws Exception {
         List<Map<String,Object>> resultSet = jdbcOperations.queryForList("SELECT * FROM COUNTRY");

         for(Map<String, Object> result : resultSet) {
             System.out.println(result);
         }

    }
}

@组件
公共类DemoRunner实现CommandLineRunner{
私有最终JdbcOperations JdbcOperations;
公共DemoRunner(JdbcOperations JdbcOperations){
this.jdbcooperations=jdbcooperations;
}
@凌驾
公共无效运行(字符串…参数)引发异常{
List resultSet=jdbcOperations.queryForList(“从国家/地区选择*);
对于(映射结果:resultSet){
系统输出打印项次(结果);
}
}
}

我可以看到代码运行了,但它没有像我预期的那样返回固定值。有人知道我哪里出错了吗?

使用
ElementMatchers.is(jdbcooperations.class)
,您正在加载
jdbcooperations
类。如果不重新传输,则不会检测此类,因为在安装代理时已加载该类


而是使用
ElementMatchers.named(“pkg.of.jdbc操作”)

ElementMatchers.is(jdbc操作.class)
一起加载
jdbc操作
类。如果不重新传输,则不会检测此类,因为在安装代理时已加载该类

而是使用
ElementMatchers.named(“pkg.of.jdbc操作”)