Spring Bean懒加载疑问:内部未标@Lazy的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:
@ConditionalOnPropertydetermines whether the Bean is registered in the Spring container at all. The Bean only exists ifpublish.feed.events=trueis 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 ifKafkaPublisherdoesn't have@Lazy. This is becauseEventPublishServiceneeds a fully initialized instance ofKafkaPublisherto 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@Lazyand 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 ifKafkaPublisherhas no@Lazy). - Any dependencies that
KafkaPublisherhas 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

