Recently i found spring documentation page that says:
Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice).
If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used.
But it doesn't seem to be the case in my application. I wanted to write a small benchmark to compare the performance of both types of proxying.
There are two similar classes. Both have one method annotated with the @Transactional annotation. One class implements the interface and the other does not:
@Service public class Cglib { @Transactional public void method() {} } public interface Dynamic { void method(); } @Service public class DynamicImpl implements Dynamic { @Override @Transactional public void method() {} } And based on the documentation for the first class, a CGLIB proxy should be created, and for the second, a JDK dynamic proxy.
But in my case CGLIB was used for both classes:
@Component @RequiredArgsConstructor public class Runner implements ApplicationRunner { private final Cglib cglib; private final Dynamic dynamic; @Override public void run(ApplicationArguments args) { System.out.println(cglib.getClass()); System.out.println(dynamic.getClass()); } } class com.example.demo.proxy.cglib.Cglib$$EnhancerBySpringCGLIB$$767ff22 class com.example.demo.proxy.dynamic.DynamicImpl$$EnhancerBySpringCGLIB$$20a564d6 There are no additional configurations in the application. Only @SpringBootApplication class generated via spring initializr
Am I doing something wrong? The code was run on Spring Boot 2.7.2 and JDK 17.