You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Bean懒加载疑问:内部未标@Lazy的Bean是否也懒加载?

懒加载Bean的依赖初始化规则详解

Hey there, let's break down your question clearly:

First, the core rule: When a lazy-loaded Bean is triggered to initialize for the first time, Spring will automatically initialize all its dependencies—regardless of whether those dependencies are marked with @Lazy or not. But we need to split this into two parts, especially considering your specific code setup.

1. Your Scenario: @ConditionalOnProperty + Lazy Loading

Your EventPublishService uses @ConditionalOnProperty, which is a separate concept from lazy loading:

  • @ConditionalOnProperty determines whether the Bean is registered in the Spring container at all. The Bean only exists if publish.feed.events=true is set in your config.
  • Lazy loading (via @Lazy) determines when the registered Bean is initialized—it waits until the first time the Bean is used.

If your EventPublishService is also lazy-loaded (e.g., you add @Lazy to it):

  • It won't initialize until you first call or inject it somewhere.
  • When it does initialize, Spring will immediately initialize its dependent KafkaPublisher—even if KafkaPublisher doesn't have @Lazy. This is because EventPublishService needs a fully initialized instance of KafkaPublisher to function properly.

2. Pure Lazy Loading Scenario Rules

For a standard lazy-loaded Bean:

  • The Bean itself is delayed until first use.
  • When it initializes, all its direct dependencies (via @Autowired, constructor injection, etc.) are initialized immediately—unless the dependency is also marked with @Lazy and uses proxy mode (like constructor injection with @Lazy, which creates a proxy that only initializes the dependency when its methods are called).

Example 1: Non-lazy dependency gets initialized with the lazy Bean

@Service
@Lazy
public class LazyService {
    // NonLazyService is initialized as soon as LazyService is initialized
    @Autowired
    private NonLazyService nonLazyService;
}

@Service
public class NonLazyService {}

When LazyService is first used, NonLazyService is initialized right along with it.

Example 2: Lazy dependency with proxy (delayed initialization)

@Service
@Lazy
public class LazyService {
    private final LazyDependency lazyDependency;

    // @Lazy here creates a proxy—LazyDependency isn't initialized until its methods are called
    public LazyService(@Lazy LazyDependency lazyDependency) {
        this.lazyDependency = lazyDependency;
    }
}

@Service
@Lazy
public class LazyDependency {}

In this case, LazyDependency won't initialize when LazyService does—it waits until you actually invoke a method on lazyDependency.

Applying This to Your Code

If your EventPublishService is registered (because publish.feed.events=true) and lazy-loaded:

  • The first time you use EventPublishService, Spring initializes it, and along with it, KafkaPublisher (even if KafkaPublisher has no @Lazy).
  • Any dependencies that KafkaPublisher has will also be initialized in sequence, forming a chain.

To sum up: Dependencies of a lazy-loaded Bean are loaded when the lazy Bean first initializes, unless the dependency itself is configured for lazy initialization via @Lazy (using proxy mode).

内容的提问来源于stack exchange,提问作者Madhur Ahuja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:43:16