Spring IoC 容器
了解 Spring 如何创建并装配 Bean
Spring IoC 容器 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Inversion of Control
Inversion of Control (IoC) means your objects no longer create their own collaborators. Instead, a container constructs them and hands over (injects) their dependencies.
This flips the traditional flow: rather than calling new everywhere, you declare what you need and let the framework wire it up.
The ApplicationContext
Spring’s IoC container is the ApplicationContext. It reads bean definitions, instantiates them, resolves dependencies, and manages their lifecycle from creation to shutdown.
In Spring Boot, SpringApplication.run builds this context for you and keeps it running.
What Is a Bean?
A bean is simply an object that the container creates and manages. The context knows how to build it, what it depends on, and when to destroy it.
Beans are typically singletons living for the life of the application, though other scopes exist.
Declaring Beans with @Component
Annotate a class with @Component and component scanning will register it. Specializations like @Service, @Repository, and @Controller are @Component with added semantics.
@Service
public class OrderService {
public void placeOrder() { /* ... */ }
}
@Repository
public class OrderRepository { }Component Scanning
@SpringBootApplication includes @ComponentScan, which searches the main class’s package and sub-packages for stereotype annotations. Keep your classes under that root package so they are discovered.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Declaring Beans with @Bean
For types you do not own or want to configure manually, define a factory method annotated @Bean inside a @Configuration class. The return value becomes a managed bean.
@Configuration
public class AppConfig {
@Bean
public RestClient restClient() {
return RestClient.builder()
.baseUrl("https://api.example.com")
.build();
}
}@Component vs @Bean
Both create beans, but differ in control:
@Component— you own the class and let scanning register it@Bean— explicit factory method, ideal for third-party classes or conditional construction
Dependency Injection in Action
The container reads a bean’s constructor parameters and supplies matching beans. Here OrderService receives a fully built OrderRepository automatically.
@Service
public class OrderService {
private final OrderRepository repo;
public OrderService(OrderRepository repo) {
this.repo = repo;
}
}Resolving by Type
Spring injects by type. If exactly one bean matches the parameter type, it is wired in. The bean’s concrete class can implement an interface, and you inject the interface.
@Service
public class NotificationService {
private final MessageSender sender; // interface
public NotificationService(MessageSender sender) {
this.sender = sender; // gets the single impl bean
}
}Handling Multiple Candidates
When several beans match a type, disambiguate with @Primary on the default, or @Qualifier at the injection point to name the exact one.
@Bean @Primary
MessageSender emailSender() { return new EmailSender(); }
@Bean
MessageSender smsSender() { return new SmsSender(); }
// inject a specific one
public Svc(@Qualifier("smsSender") MessageSender s) { }Why IoC Helps
Delegating wiring to the container brings real benefits:
- Testability — inject mocks instead of real collaborators
- Loose coupling — depend on interfaces, not constructors
- Centralized lifecycle — one place manages creation and shutdown
Quick Check
Test your understanding of bean declaration.
Recap
The IoC container wires your application together.
- IoC means the container, not your code, creates and injects collaborators
- The
ApplicationContextmanages beans @Component+ scanning registers your classes;@Beanhandles others- Injection resolves by type, disambiguated with
@Primary/@Qualifier
常见问题解答
「Spring IoC 容器」课时是免费的吗?
是的 — 「Spring IoC 容器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
「Spring IoC 容器」这节课中我会学到什么?
了解 Spring 如何创建并装配 Bean 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Spring IoC 容器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?
能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Spring IoC 容器
- 构造函数注入与字段注入
- Bean 作用域
- 生命周期回调