0
0
Spring Bootframework~8 mins

Custom auto-configuration in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom auto-configuration
MEDIUM IMPACT
Custom auto-configuration affects application startup time and memory usage by adding or skipping bean creation during Spring Boot app initialization.
Adding custom auto-configuration to a Spring Boot app
Spring Boot
package com.example.autoconfig;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(HeavyService.class)
@ConditionalOnProperty(prefix = "myapp.heavy", name = "enabled", havingValue = "true", matchIfMissing = false)
public class MyAutoConfig {

    @Bean
    public HeavyService heavyService() {
        return new HeavyService();
    }
}
Creates HeavyService bean only if class is present and property enabled, reducing startup overhead.
📈 Performance GainAvoids unnecessary bean creation; startup faster by 100-200ms; saves memory
Adding custom auto-configuration to a Spring Boot app
Spring Boot
package com.example.autoconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAutoConfig {

    @Bean
    public HeavyService heavyService() {
        return new HeavyService();
    }
}
Always creates HeavyService bean regardless of app needs, increasing startup time and memory use.
📉 Performance CostBlocks startup for extra 100-200ms; adds unnecessary memory usage
Performance Comparison
PatternBean CreationStartup DelayMemory UsageVerdict
Unconditional bean creationAlways creates beanAdds 100-200ms delayHigher memory use[X] Bad
Conditional bean creationCreates bean only if neededNo extra delay if skippedLower memory use[OK] Good
Rendering Pipeline
During Spring Boot startup, auto-configuration classes are scanned and conditionally loaded. Beans are created if conditions match, affecting initialization time and memory allocation.
Bean Definition Loading
Bean Creation
Application Context Initialization
⚠️ BottleneckBean Creation stage is most expensive when unnecessary beans are created.
Optimization Tips
1Use @Conditional annotations to load beans only when needed.
2Avoid creating heavy beans unconditionally to reduce startup time.
3Measure startup performance with Spring Boot Actuator tools.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using conditional annotations in custom auto-configuration?
AThey delay bean creation until the first method call.
BThey increase the number of beans created to improve functionality.
CThey prevent unnecessary bean creation, reducing startup time and memory use.
DThey automatically optimize database queries.
DevTools: Spring Boot Actuator / Application Startup Analyzer
How to check: Enable Spring Boot Actuator and use the startup endpoint or ApplicationStartup API to measure bean creation times and startup phases.
What to look for: Look for beans that take long to create or are created unnecessarily; check if conditional annotations reduce startup time.