A Spring bean is an object managed by the Spring IoC container. Bean instantiation is the process of creating a new bean object — just like creating an object from a class.
Bean-stantiation
Normally, instantiating an object requires the new keyword. Say we’re designing a racing game —
each round needs a race track and drivers:
public class RaceTrack { private String location; private int miles; private String trackType;}
public class Driver { private String name; private String team; private int yearsExperience;}A class to represent each round:
public class RaceRound { private String startTime; private RaceTrack currentRaceTrack = new RaceTrack(); private Driver currentDriver = new Driver();}Each round instantiates a RaceTrack and Driver with new. Any change to those two classes
means additional changes wherever they’re instantiated, like in RaceRound.
With Spring beans: mark RaceTrack and Driver as beans with @Component:
@Componentpublic class RaceTrack { private String location; private int miles; private String trackType;}
@Componentpublic class Driver { private String name; private String team; private int yearsExperience;}Then remove the manual instantiation from RaceRound, using @Autowired instead:
public class RaceRound { private String startTime; @Autowired private RaceTrack currentRaceTrack; @Autowired private Driver currentDriver;}No more new when declaring RaceTrack/Driver instances — marking them as Spring beans lets
the IoC container manage them: instantiate them and inject them into RaceRound. As a bonus, we
didn’t have to change any existing code in RaceTrack/Driver — just add @Component.
(@Component and @Autowired are just two of many annotations for working with Spring beans and
the IoC container.)
Auto-bean loading
The fully automatic approach uses three annotations:
@Configuration— tells the framework beans may be created via the annotated class.@ComponentScan— tells the framework to scan the code for components (classes, controllers, services, etc).@EnableAutoConfiguration— tells the container to auto-create beans from the components found.
Together: where to look, how to search, and how to auto-instantiate. All three are bundled into a
single annotation — @SpringBootApplication — which combines @Configuration,
@ComponentScan, and @EnableAutoConfiguration. Apply it to the class holding main, and the
app runs with all of this built in — on startup, the container scans the code for components to
turn into beans.
@SpringBootApplicationpublic class RecipeApplication {
public static void main(String[] args) { SpringApplication.run(RecipeApplication.class, args); }}Earlier we declared dependencies as fields annotated with @Autowired. In other Spring
applications you’ll often see dependencies injected via the constructor instead (no annotation
needed):
public class CoffeeController {
private final CoffeeRepository coffeeRepository;
public CoffeeController(CoffeeRepository coffeeRepo) { this.coffeeRepository = coffeeRepo; }}Just to illustrate: there are several ways the IoC container wires up Spring beans in an application.