How to create a bean using Bean annotation in Spring Boot for abstract class?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You cannot instantiate an abstract class directly, so Spring cannot create a bean from the abstract class itself alone. What you can do is declare a @Bean method whose return type is the abstract class and have that method return a concrete subclass instance. That is the usual solution when you want the rest of the application to depend on the abstract type.
The Key Rule
This does not work conceptually:
- abstract class as the thing Spring should instantiate directly
This does work:
- abstract class as the exposed bean type
- concrete implementation as the object actually returned
That distinction is the entire answer.
Example With A Concrete Subclass
Suppose you have an abstract service:
And a concrete implementation:
Now expose it from a configuration class:
Spring manages the returned StripePaymentService instance, but any injection point can depend on PaymentService.
Injecting The Abstract Type
Once the bean is registered, inject the abstract type normally.
This is exactly why returning the abstract type from the @Bean method is useful. The rest of the application depends on the abstraction, not the concrete class.
You Can Also Use Constructor Arguments
A @Bean method can build the concrete subclass with dependencies.
This is common when the implementation class is not annotated with @Component or when you want full construction control.
What About Anonymous Subclasses
Technically, you can return an anonymous subclass from a @Bean method.
This works, but it is usually a poor long-term design. Anonymous subclasses are harder to test, document, and reuse. Prefer a named concrete implementation unless the logic is trivial and truly local.
Multiple Implementations Need Qualifiers
If you define more than one bean of the same abstract type, plain injection becomes ambiguous.
Now Spring needs help choosing one. Use @Qualifier or @Primary depending on the design.
When @Bean Is Better Than @Component
Use @Bean when:
- the implementation class is from a third-party library
- construction logic is conditional or customized
- you want to expose an abstract type while choosing the implementation in configuration
Use @Component when the concrete class is yours and straightforward to discover by component scanning.
Common Pitfalls
- Expecting Spring to instantiate the abstract class itself.
- Returning
nullor an incomplete placeholder from the@Beanmethod. - Creating multiple beans of the same abstract type without qualifiers or a primary bean.
- Using anonymous subclasses when a named implementation would be clearer.
- Confusing the bean's exposed type with the actual class being instantiated.
Summary
- Spring cannot instantiate an abstract class directly.
- A
@Beanmethod can return the abstract type while creating a concrete subclass instance. - This allows the rest of the application to depend on the abstraction.
- Use qualifiers or
@Primaryif multiple implementations exist. - Prefer named concrete classes over anonymous subclasses for maintainability.

