๐ก Spring Framework Core Archi: IoC/DI & AOP โ Executive Summary
Technical architectural analysis, practical configuration manifests, and deployment instructions for Spring Framework Core Archi: IoC/DI & AOP.
Subpage
Related pages
Spring load order
- Servlet container initialization: Servlet containers such as Tomcat and Jetty are initialized and servlets are configured.
- Register and initialize DispatcherServlet: The servlet container loads and initializes DispatcherServlet.
- Initialization of Spring container (WebApplicationContext): DispatcherServlet initializes Spring container (ApplicationContext) and loads beans in the application.
- Bean creation and initialization: The Spring container creates beans and initializes them through dependency injection.
- Ready to process requests: The servlet container is ready to receive and process HTTP requests.
SpringBoot load order
- JVM startup and main method execution: The main() method of the main class tagged with @SpringBootApplication is executed.
java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- Creating and setting up a SpringApplication object: The execution environment and settings of Spring Boot are determined.
- Prepare ApplicationContext: The application’s bean configuration and environment settings are prepared.
- Auto-Configuration: Required settings are automatically applied based on dependencies.
โ If the spring-boot-starter-web library is included, Spring MVC is automatically configured and the built-in Tomcat server runs.
โ If spring-boot-starter-data-jpa is included, Hibernate is automatically configured and the JPA repository is prepared. - Embedded web server initialization (Tomcat, etc.): If it is a web application, the embedded servlet container is initialized.
โ Embedded servlet containers (Tomcat, Jetty, Undertow, etc.) are automatically configured and started. This process sets up HTTP ports, servlet mappings, filters, etc. - ApplicationContext initialization and bean creation: Beans are scanned and created and dependency injection is performed.
โ @Component, @Service, @Repository, @Controller
โ @Configuration > @Bean (external dependency โ Library) - Execute CommandLineRunner, ApplicationRunner: Additional execution logic is executed.
java
@SpringBootApplication
public class MyApplication implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Application started!");
}
}
- Application execution complete: All settings are completed and the application enters the running state.
- HTTP request processing: When a client’s request comes in, it is processed through DispatcherServlet.
Spring Container
Implementation
References and Guidelines (NOTE)
BeanFactory, ApplicationContext, and WebApplicationContext are the main container implementations provided by Spring Framework, and each has different characteristics and roles. Below is a summary of their differences and comparisons.
1. BeanFactory
- Description
- It is the most basic container of Spring Framework and provides **DI (Dependency Injection)** and Bean life cycle management.
- Lazy Initialization is the default, and beans are created when needed.
- Provides minimal functionality, so it is memory efficient but lacks additional features.
- Main Features
- Lazy initialization: Bean is not created until requested.
- Lightweight container: Container for basic DI and bean management of applications.
- No additional features: Does not provide advanced features such as event handling, message sources, or internationalization.
- Use example
java
java
์ฝ๋ ๋ณต์ฌ
Resource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource); // Deprecated
MyBean bean = factory.getBean(MyBean.class);
2. ApplicationContext
- Description
- This is a container that extends BeanFactory and supports advanced features and application-wide settings.
- Includes all features of BeanFactory and provides additional features such as event handling, message sources, internationalization, and AOP.
- Container used by default in Spring applications.
- Main Features
- Immediate initialization: Initializes all Singleton beans at application startup (lazy initialization is possible).
- Provides advanced features:
- Event handling (
ApplicationEvent). - Internationalization support (
MessageSource). - Integrated with AOP.
- Event handling (
- Type:
ClassPathXmlApplicationContext: Reads XML files in the class path.FileSystemXmlApplicationContext: Reads the XML file in the file system path.AnnotationConfigApplicationContext: Use Java Config.
- Use example
java
java
์ฝ๋ ๋ณต์ฌ
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean bean = context.getBean(MyBean.class);
3. WebApplicationContext
- Description
- A sub-interface of ApplicationContext, it provides additional functions specialized for web applications.
- Used in web applications such as Spring MVC, integrated with ServletContext.
- Spring’s DispatcherServlet initializes and manages the WebApplicationContext.
- Main Features
- Web environment integration: Integrated with ServletContext to support web application-specific features.
- Root Context and Servlet Context can be separated:
- Root Context: Commonly used beans (e.g. database connection).
- Servlet Context: Beans (e.g. controller) required for a specific servlet.
- Additional features:
- Bean management integrated with servlet.
- Web application scope support (
request,session,applicationscopes).
- Implementation:
XmlWebApplicationContext: Based on XML configuration.AnnotationConfigWebApplicationContext: Based on Java Config.
- Example of use
Initialized by DispatcherServlet:
xml
xml
์ฝ๋ ๋ณต์ฌ
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
Reference WebApplicationContext from ServletContext:
java
java
์ฝ๋ ๋ณต์ฌ
ServletContext servletContext = request.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
MyBean bean = context.getBean(MyBean.class);
4. Compare key differences
| Features | BeanFactory | ApplicationContext | WebApplicationContext |
| Purpose | Basic DI and bean life cycle management | Supports application-wide settings and advanced features | Provides web application specialized functions |
| Initialization time | Lazy Initialization | Initialize when application starts | Initialized by DispatcherServlet when web application starts |
| Additional features | None | Event handling, internationalization, AOP support | Integration with ServletContext, web scope support |
| Main implementation | XmlBeanFactory | ClassPathXmlApplicationContext, AnnotationConfigApplicationContext | XmlWebApplicationContext, AnnotationConfigWebApplicationContext |
| Usage environment | simple application | Typical Spring application | Spring MVC, web application |
| Integration with ServletContext | None | None | Works in an integrated way |
5. Selection criteria
- BeanFactory:
- Used when bean management is needed in simple applications or test code (currently rarely used).
- ApplicationContext:
- Container used by default in typical Spring applications.
- WebApplicationContext:
- Used to provide necessary functions (ServletContext integration, web scope, etc.) in web applications (Spring MVC).
6. Conclusion
- BeanFactory is the most basic form of Spring Container, and is currently mainly used internally or in simple environments.
- ApplicationContext is an extension of BeanFactory, a standard container used in typical Spring applications.
- WebApplicationContext extends ApplicationContext to provide features specialized for web applications and is used in web environments such as Spring MVC.
In practice, ApplicationContext or WebApplicationContext is mostly used, and BeanFactory is rarely used.




Leave a Reply