Dependency injection means a class does not build the objects it depends on. It asks for them, and someone else provides them. In Spring, that someone is the container, which creates your objects at startup and connects them, so your classes stay small, easy to test and easy to change.

A class that builds its own helpers

Our SignupService registers a user: it saves them in a repository and sends a welcome message through an email notifier. In its first version, it builds both helpers itself:

private final UserRepository users = new InMemoryUserRepository();
private final EmailNotifier notifier = new EmailNotifier();

HardWiredSignupService.java

It works, but every test of register sends a real email, and switching to text messages means editing the signup service. You also only learn what the class needs by reading its body. All three problems have one cause: the class builds its own collaborators.

Ask instead of build

Delete the new calls and ask for the helpers in the constructor:

public SignupService(UserRepository users, Notifier notifier) {
    this.users = users;
    this.notifier = notifier;
}

SignupService.java

Both parameter types are interfaces, so the service no longer knows about email, only that something can send a message. A test can now pass a fake notifier that records messages in a list: plain JUnit, no mail server, no framework.

Inversion of control

Someone still calls new; we only moved it, usually to main. over creating the helpers has moved out of the class. That is inversion of control, and dependency injection is a specialized form of it.

Wiring three objects by hand is easy. Wiring two hundred in the right order is bookkeeping, and that is the job a container takes over.

Spring does the wiring

Put @Service on the signup service and @Component on the notifier and the repository. Nothing else changes: same constructor, no @Autowired.

At startup, component scanning finds these classes in the application’s package and below and makes each one a bean, an object the container manages. To build the signup service, Spring reads its only constructor, finds one bean of each parameter type, creates those first, and makes the same call we wrote in main.

Because this happens at startup, mistakes fail early. Remove @Component from the notifier and the app refuses to start: parameter 1 of the constructor required a bean of type Notifier that could not be found. Add a second notifier and it also refuses: there are two candidates, and Spring suggests @Primary or @Qualifier instead of guessing.

The container without Spring Boot

Boot hides the container, but you can build it yourself in plain Java:

try (var context = new AnnotationConfigApplicationContext(AppConfig.class)) {
    SignupService signup = context.getBean(SignupService.class);
    signup.register("Ada");
    // ...
}

ContainerApp.java

That context is the container, the same kind of object Spring Boot creates at startup. It reads AppConfig, creates the beans, and hands us a finished signup service.

AppConfig is a @Configuration class with @ComponentScan, which scans from its own package downwards. A @Component class in a sibling package is never found, and asking for it throws NoSuchBeanDefinitionException. When a bean is missing, check the package first.

@Component or @Bean

AppConfig also creates a bean for a class we cannot annotate. Look at the method:

@Bean
public Clock clock() {
    return Clock.systemUTC();
}

AppConfig.java

Clock lives in java.time, so Spring calls this method and keeps the result as a bean named clock.

The rule: your own class with a plain constructor gets @Component; a library class, or one that needs real setup, gets a @Bean method. Both become the same kind of bean, a singleton by default, and you can mix them. @Import pulls in more configuration classes, and beans connect across them by type.

Why @Configuration matters

In PersistenceConfig, the repository’s bean method calls the audit-log bean method directly, yet only one audit log exists. Spring subclasses every @Configuration class with CGLIB, and that subclass returns the existing bean instead of running your method again.

Turn that off with proxyBeanMethods = false and the same call becomes plain Java. The audit log is created twice, and the repository holds one the container does not manage. The fix that works in every mode is to take the audit log as a parameter:

@Bean
public UserRepository userRepository(AuditLog auditLog) {
    return new InMemoryUserRepository(auditLog);
}

LiteFixedConfig.java

Spring passes the one managed audit log in, with or without the CGLIB subclass.

Back in Boot, @SpringBootApplication is @SpringBootConfiguration (itself a @Configuration), @EnableAutoConfiguration and @ComponentScan. adds Boot’s own beans to the container you now know. The Spring Framework reference covers @Bean and @Configuration in full.

Recap

  • A class asks for what it needs in its constructor; it does not build those objects itself.
  • The container creates the beans and passes them in. A single constructor needs no @Autowired.
  • A missing or duplicate bean fails at startup, with a message naming the type.
  • Use @Component for your own classes and a @Bean method for library types or anything that needs setup.
  • In @Bean methods, take collaborators as parameters; it works with or without proxyBeanMethods.

Sources

Official documentation and well-known tutorials read while writing this article. The wording here is our own.