9.2.13. Bean Proxies

9.2.13.1. About Bean Proxies

A proxy is a subclass of a bean, which is generated at runtime. It is injected at bean creation time, and dependent scoped beans can be injected from it, because the lifecycles of the dependent beans are tied to proxy. Proxies are used as a substitute for dependency injection, and solve two different problems.

Problems of dependency injection, which are solved by using proxies

  • Performance - Proxies are much faster than dependency injection, so you can use them in beans which need good performance.
  • Thread safety - Proxies forward requests to the correct bean instance, even when multiple threads access a bean at the same time. Dependency injection does not guarantee thread safety.

Types of classes that cannot be proxied

  • Primitive types or array types
  • Classes that are final or have final methods
  • Classes which have a non-private default constructor