How Spring implements Singleton Beans

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

In the next example, the dataSource method is a bean that is called twice. However, we know that a bean is a singleton by default in Spring (see the link below):

图片[1]-How Spring implements Singleton Beans - 拾光赋-拾光赋

Spring Configuration

eidher ・ Sep 27 ’20 ・ 2 min read

#spring #java #dependencyinjection #bean

@Configuration
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        return new JdbcDataSource();
    }

    @Bean
    public DevRepository devRepository() {
        JdbcDevRepository repository = new JdbcDevRepository(dataSource);
        repository.setDataSource(dataSource());
        return repository;
    }

    @Bean
    public ProdRepository prodRepository() {
        JdbcProdRepository repository = new JdbcProdRepository();
        repository.setDataSource(dataSource());
        return repository;
    }
}

Enter fullscreen mode Exit fullscreen mode

At startup time, a subclass is created using cglib (Code Generation Library). It only calls super in the first invocation of the bean, then one instance is cached by the application context and the child class is the entry point.

@Configuration
public class AppConfig$$EnhancerByCGLIB$ extends AppConfig{
...
}

Enter fullscreen mode Exit fullscreen mode

However, cglib does not support classes with constructors (autowired constructors are possible since Spring 4.3. See Spring Injection Types), so Spring now uses objenesis to overcome this.

图片[1]-How Spring implements Singleton Beans - 拾光赋-拾光赋

Singleton Pattern

eidher ・ Oct 1 ’20 ・ 1 min read

#creational #designpattern #pattern #java

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

原文链接:How Spring implements Singleton Beans

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

请登录后发表评论

    暂无评论内容