# Fix Java Spring Bean Creation Exception
Your Spring application fails to start with BeanCreationException. The error message indicates that Spring couldn't create a bean due to dependency issues, configuration problems, or other failures.
Introduction
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myService':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExceptionCommon causes: - Missing bean definition - Circular dependency - Constructor injection failure - Missing required properties - Wrong bean type
Symptoms
- Error messages appear in logs
- Service fails to respond correctly
- Unexpected behavior in production
Common Causes
- Configuration misconfiguration
- Resource exhaustion or limits
- Network connectivity issues
- Permission or access denied
Step-by-Step Fix
Enable debug logging:
# application.yml
logging:
level:
org.springframework: DEBUG
org.springframework.beans: TRACECheck bean creation:
```bash # Run with debug java -jar app.jar --debug
# Or enable actuator curl http://localhost:8080/actuator/beans | jq ```
Common Errors and Solutions
Error 1: NoSuchBeanDefinitionException
NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.MyRepository'Cause: Bean not found in application context.
Solution:
```java // 1. Add @Component, @Service, or @Repository annotation @Repository public class MyRepositoryImpl implements MyRepository { // ... }
// 2. Enable component scanning @SpringBootApplication @ComponentScan("com.example") public class Application { }
// 3. Or define bean in configuration @Configuration public class MyConfig { @Bean public MyRepository myRepository() { return new MyRepositoryImpl(); } }
// 4. Check if interface has implementation public interface MyRepository { } @Repository public class MyRepositoryImpl implements MyRepository { } ```
Error 2: Circular Dependency
BeanCurrentlyInCreationException: Error creating bean with name 'serviceA':
Requested bean is currently in creation: Is there an unresolvable circular reference?Cause: ServiceA depends on ServiceB, which depends on ServiceA.
Solution:
```java // Option 1: Use @Lazy @Service public class ServiceA { private final ServiceB serviceB;
public ServiceA(@Lazy ServiceB serviceB) { this.serviceB = serviceB; } }
// Option 2: Use setter injection instead of constructor @Service public class ServiceA { private ServiceB serviceB;
@Autowired public void setServiceB(ServiceB serviceB) { this.serviceB = serviceB; } }
// Option 3: Refactor to break the cycle @Service public class ServiceA { private final SharedService sharedService;
public ServiceA(SharedService sharedService) { this.sharedService = sharedService; } }
// Option 4: Use @PostConstruct @Service public class ServiceA { @Autowired private ServiceB serviceB;
@PostConstruct public void init() { // Use serviceB here } } ```
Error 3: Constructor Injection Failure
BeanCreationException: Could not resolve matching constructorSolution:
```java // Single constructor - @Autowired is implicit @Service public class MyService { private final MyRepository repository;
public MyService(MyRepository repository) { this.repository = repository; } }
// Multiple constructors - specify which to use @Service public class MyService { private final MyRepository repository; private String name;
@Autowired public MyService(MyRepository repository) { this.repository = repository; }
public MyService(MyRepository repository, String name) { this.repository = repository; this.name = name; } }
// Optional dependencies @Service public class MyService { private final MyRepository repository; private Optional<OtherService> otherService;
@Autowired public MyService(MyRepository repository, @Autowired(required = false) OtherService otherService) { this.repository = repository; this.otherService = Optional.ofNullable(otherService); } } ```
Error 4: Missing Required Properties
BeanCreationException: Error creating bean: 'dataSource'
Failed to bind properties under 'spring.datasource'Solution:
# application.yml - Add required properties
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: user
password: password
driver-class-name: com.mysql.cj.jdbc.Driver```java // Or use @Value with default @Service public class MyService { @Value("${my.property:default-value}") private String myProperty; }
// Or use @ConfigurationProperties @ConfigurationProperties(prefix = "my") public class MyProperties { private String property; // getter/setter } ```
Error 5: Wrong Bean Type
BeanNotOfRequiredTypeException: Bean named 'myService' is expected to be of type 'com.example.MyService'
but was actually of type 'com.example.MyServiceImpl'Solution:
```java // Inject interface, not implementation @Service public class MyServiceImpl implements MyService { }
// Correct @Service public class OtherService { private final MyService myService; // Interface
public OtherService(MyService myService) { this.myService = myService; } }
// Wrong @Service public class OtherService { private final MyServiceImpl myService; // Implementation } ```
Error 6: Multiple Bean Candidates
NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.Repository'
available: expected single matching bean but found 2: repoA,repoBSolution:
```java // Option 1: Use @Qualifier @Service public class MyService { private final Repository repository;
public MyService(@Qualifier("repoA") Repository repository) { this.repository = repository; } }
// Option 2: Use @Primary @Repository @Primary public class PrimaryRepository implements Repository { }
// Option 3: Use bean name @Service public class MyService { @Autowired @Qualifier("repoA") private Repository repository; }
// Option 4: Use specific type @Service public class MyService { private final RepoA repoA; // Specific implementation
public MyService(RepoA repoA) { this.repoA = repoA; } } ```
Error 7: Profile-Specific Bean Missing
NoSuchBeanDefinitionException: No qualifying bean for Profile-specific beanSolution:
```java // Bean only created for specific profile @Service @Profile("production") public class ProductionService implements MyService { }
@Service @Profile("development") public class DevelopmentService implements MyService { }
// Activate profile // application.yml spring: profiles: active: production
// Or command line java -jar app.jar --spring.profiles.active=production ```
Error 8: Conditional Bean Not Created
NoSuchBeanDefinitionException: Bean not created due to @ConditionalSolution:
```java // Check condition @Bean @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") public MyService myService() { return new MyServiceImpl(); }
// Ensure property is set // application.yml feature: enabled: true
// Or use @ConditionalOnMissingBean @Bean @ConditionalOnMissingBean public MyService defaultMyService() { return new DefaultMyService(); } ```
Debugging Bean Issues
```java // Print all beans @Component public class BeanDebugger implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { String[] beans = event.getApplicationContext().getBeanDefinitionNames(); for (String bean : beans) { System.out.println(bean); } } }
// Check specific bean @Component public class BeanChecker { @Autowired private ApplicationContext context;
public void checkBean(String name) { try { Object bean = context.getBean(name); System.out.println("Bean found: " + bean.getClass()); } catch (Exception e) { System.out.println("Bean not found: " + name); } } } ```
Verification
```bash # Run with debug logging java -jar app.jar --debug
# Check actuator beans endpoint curl http://localhost:8080/actuator/beans | jq '.contexts.application.beans | keys'
# Check conditions report curl http://localhost:8080/actuator/conditions | jq
# Test specific bean curl -X POST http://localhost:8080/actuator/shutdown ```
Prevention
- 1.[ ] Check if bean class has @Component/@Service/@Repository
- 2.[ ] Verify component scanning includes package
- 3.[ ] Check for circular dependencies
- 4.[ ] Verify required properties are set
- 5.[ ] Use interface types for injection
- 6.[ ] Use @Qualifier for multiple candidates
- 7.[ ] Check profile is active for profile-specific beans
- 8.[ ] Verify @Conditional conditions are met
- 9.[ ] Enable debug logging for more details
- 10.[ ] Check actuator beans endpoint
- 11.## Common Causes
- Configuration misconfiguration
- Missing or incorrect credentials
- Network connectivity issues
- Version compatibility problems
- Resource exhaustion or limits
- Permission or access denied
Related Articles
- [Technical troubleshooting: Fix Beanvalidation Constraintviolationexception En](beanvalidation-constraintviolationexception-entity-persist)
- [Technical troubleshooting: Fix Classnotfoundexception Maven Shade Plugin Relo](classnotfoundexception-maven-shade-plugin-relocation)
- [Technical troubleshooting: Fix Concurrentmodificationexception Arraylist Iter](concurrentmodificationexception-arraylist-iteration)
- [Fix Ehcache Cache Eviction Memory Overflow Fix Issue in Java](ehcache-cache-eviction-memory-overflow-fix)
- [Fix Java Ehcache Eviction Memory Threshold Fix Issue in Java](java-ehcache-eviction-memory-threshold-fix)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Java Spring Bean Creation Exception", "description": "Step-by-step guide to fix Spring BeanCreationException errors. Resolve dependency injection, circular dependencies, and bean configuration issues.", "url": "https://www.fixwikihub.com/fix-java-spring-bean-creation-exception", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-27T10:26:00.000Z", "dateModified": "2026-04-27T10:26:00.000Z" } </script>