ApplicationContext context = SpringApplication.run(AppConfig.class);
- Load and process bean definitions: @Configuration classes are processed, @Components are scanned, XML files are parsed, bean definitions are added to a BeanFactory, BeanFactoryPostProcessor beans are invoked.
- Load bean definitions
- Post process bean definitions
- Perform bean creation (for each bean): each bean is eagerly instantiated by default (unless marked as lazy), each bean goes through a post-processing phase.
- Find and create its dependencies
- Instantiate beans (dependency injection)
- Call setters (dependency injection)
- Bean Post Processors
- BeforeInit: modify a bean before initialization
- Initializer: initialization methods are called (@PostConstruct, @Resource, etc)
- AfterInit: modify a bean after initialization
- Bean ready for use
AppService service = context.getBean("appService", AppService.class); service.doSomething();
When you invoke a bean obtained from the context. If the bean is wrapped by a proxy the proxy is created during the initialization phase by a BeanPostProcessor adding behavior to the bean. It may be a JDK Proxy (interface based) or a CGLib Proxy (subclass based)
The context is closed if the application shuts down (not killed or failed), all beans are cleaned up, @PreDestroy methods are invoked, beans are released for the Garbage Collector to destroy.