Sobes.tech
Junior — Middle

How are proxy objects implemented in Spring Boot 2.x?

sobes.tech AI

Answer from AI

In Spring Boot 2.x, proxy objects are created for implementing aspect-oriented programming (AOP), transaction management, and other cross-cutting concerns. The main ways to create proxies:

  1. JDK Dynamic Proxies — created for interfaces, the proxy implements the same interfaces as the target object.
  2. CGLIB proxies — created for classes, the proxy inherits from the target class and overrides methods.

Spring automatically chooses the type of proxy depending on the presence of interfaces in the bean and the configuration. For example, if a bean implements an interface, JDK Proxy is used by default; otherwise, CGLIB.

An example configuration using the @EnableAspectJAutoProxy annotation:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true) // forcibly use CGLIB
public class AppConfig {
}

Thus, proxy objects in Spring Boot are created dynamically at runtime to inject additional behavior (such as logging, transaction management) without modifying the original bean code.