Chapter 2. Defining Beans and Using Dependency Injection
In this chapter, we will cover the following recipes:
- Defining a bean explicitly with @Bean
- Defining a bean implicitly with @Component
- Using a bean via dependency injection with @Autowired
- Using a bean directly
- Listing all beans
- Using multiple configuration classes
Introduction
Beans are at the core of Spring. They are standard Java objects instantiated and managed by Spring.
Beans are mostly used to:
- Configure Spring in some way (database connection parameters, security, and so on)
- Avoid hardcoding dependencies using dependency injection, so that our classes remain self-contained and unit testable
In this chapter, you'll learn how to define beans and use them.
Get Spring Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.