Spring Dependency Injection and Inversion of Control Container Concepts
Internal
Overview
At the heart of the Spring Framework is the core container, which comes with a configuration model and a dependency injection mechanism. Support for different application architectures, including messaging, transactions and persistence is built in top of the core container.
Inversion of Control Container
Dependency Injection
Rather than have individual components create and manage the lifecycle of their dependency components, a dependency-injected application relies on container to first create all components, then to inject them into other components that need them. Injection is typically done through constructor arguments or property setters.
Spring Dependency Injection Framework
Configuration Model
This section refers to component configuration. Configuration as in external data that is provided to the application in form of properties or environment variables, and that potentially modifies the behavior of the application, is addressed in the "Application Configuration Concepts" section.
Configuration Metadata
XML-based Configuration
Representative example of an XML configuration file.
Java-based Configuration (@Configuration). Annotation injection is performed before XML injection, thus the XML configuration overrides the annotations for properties wired through both approaches.
Configuration time. Define what that is.
Configuration class.
Component scanning
Autowiring. @Autowired.
Automatic configuration (Spring Boot autoconfiguration).
Application Context
The application context provides:
- Bean factory functionality, inherited from ListableBeanFactory.
- Ability to load file resources, inherited from ResourceLoader.
- Ability to publish events to registered listeners, inherited from ApplicationEventPublisher.
- Ability to resolve messages, inherited from MessageSource.
- Inheritance from a parent context.
The application context is created during the bootstrapping phase of a Spring application.
Is the Spring container and application context one and the same thing? Some texts claim so.
Beans
Any non-trivial application consists of many components, each responsible for its own piece of the overall application functionality. These components coordinate with each other. Spring names these these components beans. The Spring container creates and introduces the beans to each other.
Bean name
Bean alias
Collaborator beans
Describe bean initialization process. Who does it?
Stereotypes.
@Bean What is the fundamental difference between @Bean and the rest (@Component, etc.)?