Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Spring @Configuration proxyBeanMethods: direct calls can create a second object

Last updated: 1 Oct 20264 min read
tutorial
IntermediateBy AITrove Editorial

An inter-bean call to a @Bean method is intercepted by a full @Configuration class, but acts like a plain Java call when proxyBeanMethods is false.

Download Spring source kit

Watch object identity

A delivery client calls retryPolicy() while it is being built. In the full configuration, Spring intercepts that call and supplies the managed singleton. With proxyBeanMethods=false, the call constructs another RetryPolicy, separate from the bean registered under that method. The test compares identity in both contexts.

The safer configuration style is to declare RetryPolicy as a parameter of the deliveryClient bean method. Then the container resolves the dependency explicitly, and the method does not rely on proxy interception. This is especially useful when a configuration class is designed without method proxies. Bean construction and context ownership provide the surrounding model.

Different from an AOP service proxy

This is configuration-method interception at bean creation time. Transactional self-invocation concerns calls on a service instance at runtime. Both involve proxies, but fixing one does not fix the other.

Checked code

Java
@Configuration(proxyBeanMethods = false)
static class PlainPolicyConfig {
    @Bean RetryPolicy retryPolicy() { return new RetryPolicy(4); }
    @Bean DeliveryClient deliveryClient() {
        return new DeliveryClient(retryPolicy());
    }
}

Verification boundary

The Maven source kit passes WiringAndBindingContractTest.directBeanMethodCallChangesIdentityWithoutConfigurationProxy on its local Spring Boot 4 and Java 21 fixture.

Cost and ownership

The extra direct call allocates a second policy object and may repeat factory side effects. The local context test measures identity only; it does not establish performance savings for disabling configuration proxies. If a factory opens a resource, a directly created copy also lacks the expected container lifecycle ownership.

Common Mistakes

  • Do not call a sibling @Bean method under proxyBeanMethods=false when the managed singleton is required.
  • Do not assume a directly constructed object receives container lifecycle callbacks.
  • Do not confuse configuration interception with service AOP interception.

Read next

Spring @Component versus @Bean: choose who constructs the dependency, Spring ApplicationContext: construct and close an object graph, Spring AOP proxies: self-invocation bypasses proxy advice, Spring constructor injection: required dependencies stay visible.

spring
spring-boot
configuration-proxy-bean-methods
Storage details