最近我发现spring 文档页面说:
Spring AOP 使用 JDK 动态代理或 CGLIB 来为给定的目标对象创建代理。(只要有选择,JDK 动态代理都是首选)。
如果要代理的目标对象至少实现一个接口,则将使用 JDK 动态代理。
但我的申请中似乎并非如此。我想编写一个小型基准测试来比较两种代理类型的性能。
有两个类似的类。两者都有一种带有注释的方法@Transactional。一个类实现了该接口,而另一个类则没有:
@Service
public class Cglib {
@Transactional
public void method() {}
}
Run Code Online (Sandbox Code Playgroud)
public interface Dynamic {
void method();
}
Run Code Online (Sandbox Code Playgroud)
@Service
public class DynamicImpl implements Dynamic {
@Override
@Transactional
public void method() {}
}
Run Code Online (Sandbox Code Playgroud)
根据第一个类的文档,应该创建一个 CGLIB 代理,对于第二个类,应该创建一个 JDK 动态代理。
但就我而言,CGLIB 用于这两个类:
@Component
@RequiredArgsConstructor
public class Runner implements ApplicationRunner {
private final Cglib cglib;
private final Dynamic dynamic;
@Override
public void run(ApplicationArguments args) { …Run Code Online (Sandbox Code Playgroud) 我正在使用 grpc 客户端net.devh.grpc-client-spring-boot-starter
客户:
@Service
public class SystemService {
@GrpcClient("handyman-client")
StatusServiceGrpc.StatusServiceBlockingStub handymanClient;
public VersionResponse getHandymanVersion() {
return handymanClient.getVersion(Empty.newBuilder().build());
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序.yaml:
grpc:
client:
handyman-client:
address: static://localhost:8080
negotiationType: plaintext
Run Code Online (Sandbox Code Playgroud)
在运行时handymanClient是null. 3.0.0尝试了从到 的Spring Boot 版本3.0.2。降低spring boot版本2.7.0解决问题——现在正在创建客户端。
有没有办法让它在 Spring Boot 3 上工作?
构建.gradle:
@Service
public class SystemService {
@GrpcClient("handyman-client")
StatusServiceGrpc.StatusServiceBlockingStub handymanClient;
public VersionResponse getHandymanVersion() {
return handymanClient.getVersion(Empty.newBuilder().build());
}
}
Run Code Online (Sandbox Code Playgroud)
我使用此基准比较了通过正则表达式和多个单字符拆分来拆分字符串
import org.openjdk.jmh.annotations.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Test {
static String start = "1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. …Run Code Online (Sandbox Code Playgroud)