How to Autowire services in SpringBoot
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
In Spring Boot, services are usually injected through Spring's dependency injection container rather than created with new. The normal workflow is to register a service as a bean, then inject it into controllers, other services, or configuration classes using constructor injection.
Define A Service Bean
Spring can only autowire classes it knows how to create. The most common way to register a service bean is to annotate it with @Service and place it in a package scanned by Spring Boot.
@Service is a specialization of @Component, so Spring will discover it automatically during component scanning.
Inject The Service Into Another Bean
The preferred pattern in modern Spring Boot is constructor injection. The dependency is required, explicit, and easy to test.
When Spring creates GreetingController, it sees the constructor parameter, finds a matching GreetingService bean, and injects it automatically.
When @Autowired Is Needed
For a single constructor, Spring Boot does not require the @Autowired annotation. The framework can infer that the constructor should be used.
You still can use @Autowired, but for a single constructor it adds noise rather than value. It is more useful on setters, fields, or multiple constructors in older code styles.
Setter And Field Injection
Setter injection works for optional or replaceable dependencies.
Field injection is short, but it hides dependencies and makes testing harder.
For most application code, constructor injection is the better default.
Multiple Service Implementations
If more than one bean matches the same interface, Spring needs help choosing one.
Use @Qualifier when bean type alone is ambiguous.
Common Pitfalls
The most common problem is forgetting to make the service a Spring bean. If the class lacks @Service, @Component, @Bean, or another registration path, autowiring fails because Spring has nothing to inject.
Another issue is putting the class outside the application's component scan path. Spring Boot scans from the main application package downward by default.
A third mistake is using new GreetingService() manually inside application code. That bypasses Spring, so the object will not participate in dependency injection or bean lifecycle management.
Summary
- Mark service classes with
@Serviceso Spring can create them as beans. - Prefer constructor injection for required dependencies.
- '
@Autowiredis optional for a single constructor in modern Spring.' - Use
@Qualifierwhen multiple beans implement the same interface. - Avoid field injection and manual
newwhen working inside Spring-managed code.
Related reading
- How to avoid Dependency Injection constructor madness?
- How to consume a Scoped service from a Singleton?
- How to create a bean using Bean annotation in Spring Boot for abstract class?
- How to deal with a sealed class when I wanted to inherit and add properties
- How to avoid ConcurrentModificationException while removing elements from `ArrayList` while iterating it?
- How to avoid installing Unlimited Strength JCE policy files when deploying an application?
- How to define a List bean in Spring?
- How to define generic type limit to primitive types?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.