The Spring Bean Lifecycle

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

Initialization

ApplicationContext context = SpringApplication.run(AppConfig.class);

Enter fullscreen mode Exit fullscreen mode

  1. 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.
    1. Load bean definitions
    2. Post process bean definitions
  2. 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.
    1. Find and create its dependencies
    2. Instantiate beans (dependency injection)
    3. Call setters (dependency injection)
    4. Bean Post Processors
      1. BeforeInit: modify a bean before initialization
      2. Initializer: initialization methods are called (@PostConstruct, @resource, etc)
      3. AfterInit: modify a bean after initialization
    5. Bean ready for use

Usage

AppService service = context.getBean("appService", AppService.class);
service.doSomething();

Enter fullscreen mode Exit fullscreen mode

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)

Destruction

context.close();

Enter fullscreen mode Exit fullscreen mode

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.

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

原文链接:The Spring Bean Lifecycle

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容