Spring’s FactoryBean Interface

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

interface FactoryBean<T> {
    public T getObject() throws Exception;
    public Class<?> getObjectType();
    public default boolean isSingleton() { return true; }
}

Enter fullscreen mode Exit fullscreen mode

Example

public class AppServiceFactoryBean implements FactoryBean<AppService> {
    public AppService getObject() throws Exception {
        ...
        return appService:
    }

    public Class<?> getObjectType() {
        return AppService.class;
    }
}

Enter fullscreen mode Exit fullscreen mode

FactoryBeans with Java Configuration:

Spring calls getObject() automatically.

@Configuration
public class ServiceConfig {

    @Bean
    public AppServiceFactoryBean appService() {
        return new AppServiceFactoryBean();
    }

    @Bean
    public OtherService otherService(AppService appService) {
        return new OtherService(appService);
    }
}

Enter fullscreen mode Exit fullscreen mode

Examples

Used in Spring by:

  • EmbeddedDatabaseFactoryBean
  • ProxyFactoryBean
  • JndiObjectFactoryBean
  • HibernateJpaSessionFactoryBean

图片[1]-Spring's FactoryBean Interface - 拾光赋-拾光赋

Factory Method Pattern

eidher ・ Sep 29 ’20 ・ 1 min read

#creational #designpatterns #java #pattern

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

原文链接:Spring’s FactoryBean Interface

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

请登录后发表评论

    暂无评论内容