Common SpringBoot BeanCreationException Errors and How to Fix Them
This article explains the most frequent SpringBoot BeanCreationException scenarios—including missing beans, duplicate beans, constructor issues, circular dependencies, and bean overriding—provides clear code examples, and offers practical solutions to resolve each problem.
Environment: SpringBoot 3.2.5
1. Introduction
In this article we discuss BeanCreationException , a common exception thrown by the BeanFactory when creating a defined bean fails. We explore the most common causes and solutions.
2. Exception categories
2.1 NoSuchBeanDefinitionException
This occurs when Spring tries to inject a bean that does not exist in the container, for example:
<code>public class UserDAO {}
@Service
public class UserService {
@Resource
private UserDAO dao;
}</code>The UserDAO class has no annotation, causing an error when the container starts.
Check that UserDAO is annotated with @Component , @Repository , @Service , or @Controller , or defined as a @Bean , and that its package is included in component scanning.
2.2 NoUniqueBeanDefinitionException
This occurs when multiple beans of the same type exist, for example:
<code>public interface DAO {}
@Component
public class CommonDAO implements DAO {}
@Component
public class PersonDAO implements DAO {}
@Service
public class UserService {
@Resource
private DAO dao;
}</code>Solution: specify the bean name with @Resource(name="personDAO") or use @Autowired together with @Qualifier :
<code>@Service
public class UserService {
@Resource(name = "personDAO")
private DAO dao;
}</code>2.3 BeanInstantiationException
Occurs when an exception is thrown while creating an instance, for example:
<code>@Controller
public class UserController {
public UserController() {
// TODO
throw new RuntimeException("异常了");
}
}</code>It also happens when an abstract class is defined as a bean:
<code>@Controller
public abstract class UserController {
public UserController() {}
}</code>2.4 NoSuchBeanDefinitionException (constructor)
If a bean only has a parameterized constructor and the container cannot find a matching bean for the parameter type, the exception is thrown, for example:
<code>@Component
public class User {
public User(String name) {
System.out.println(name);
}
}</code>Check that a String -type bean is defined in the container.
2.5 NotWritablePropertyException
This occurs when a property has no setter and you try to set it via BeanDefinition registration, for example:
<code>public class UserService {
private DAO dao;
}</code> <code>ConfigurableApplicationContext context = ...;
context.registerBean("userService", UserService.class, bd -> {
bd.getPropertyValues().add("dao", xxx);
});</code>Running the program results in an error.
2.6 BeanCurrentlyInCreationException
Usually caused by circular constructor injection, for example:
<code>@Component
public class A {
public A(B b) {}
}
@Component
public class B {
public B(A a) {}
}</code>Solution: add @Lazy to one side of the dependency:
<code>public class A {
public A(@Lazy B b) {}
}</code>2.7 BeanDefinitionOverrideException
Thrown when two beans have the same name and overriding is disabled. Example:
<code>@Component("xxxooo")
public class A {}
@Component("xxxooo")
public class B {}
</code>Configuration to disallow overriding:
<code>spring:
main:
allow-bean-definition-overriding: false
</code>If overriding is enabled, the later bean replaces the earlier one.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.