Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are BeanFactory and FactoryBean in Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇内容主要讲解"Java中什么是BeanFactory和FactoryBean",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中什么是BeanFactory和FactoryBean"吧!

1.前提概要

很多java开发者在使用Spring框架中都见过后缀为FactoryBean的类,比如Mybatis-Spring中的SqlSessionFactoryBean。说到这里就不得不提BeanFactory。FactoryBean和BeanFactory特别容易让人混淆,面试还经常问到这两种概念。其实它们的作用和使用场景是不一样的

2.BeanFactory

先来说说BeanFactory。 用于访问Spring bean容器的根接口。这是Spring bean容器的基本客户端视图。原来是获取Spring Bean的接口,也就是IoC容器。然后我们看类图

原来我们更常用的ApplicationContext就是一个BeanFactory。我们通过bean的名称或者类型都可以从BeanFactory来获取bean。对于BeanFactory这么介绍相信都不陌生了。让我们把关注点转向FactoryBean上。

3.FactoryBean

FactoryBean 是个什么玩意儿呢?来看看源码

public interface FactoryBean { @Nullable T getObject() throws Exception; @Nullable Class getObjectType(); default boolean isSingleton() { return true; } }

T getObject() 获取泛型T的实例。用来创建Bean。当IoC容器通过getBean方法来FactoryBean创建的实例时实际获取的不是FactoryBean 本身而是具体创建的T泛型实例。等下我们会来验证这个事情。

Class getObjectType() 获取 T getObject()中的返回值 T 的具体类型。这里强烈建议如果T是一个接口,返回其具体实现类的类型。

default boolean isSingleton() 用来规定 Factory创建的的bean是否是单例。这里通过默认方法定义为单例。

3.1 FactoryBean使用场景

FactoryBean 用来创建一类bean。比如你有一些同属鸟类的bean需要被创建,但是它们自己有各自的特点,你只需要把他们的特点注入FactoryBean中就可以生产出各种鸟类的实例。举一个更加贴近实际生产的例子。甚至这个例子你可以应用到实际java开发中去。我们需要自己造一个定时任务的轮子。用FactoryBean 再合适不过了。我们来用代码说话一步步来演示FactoryBean的使用场景。

3.2 构建一个FactoryBean

我们声明定时任务一般具有下列要素:

时间周期,肯定会使用到cron表达式。

一个任务的执行抽象接口。

定时任务具体行为的执行者。

Task任务执行抽象接口的实现。实现包含两个方面:

SomeService 是具体任务的执行逻辑。

cron时间表达式

public class CustomTask implements Task { private SomeService someService; private String cronExpression; public CustomTask(SomeService someService) { this.someService = someService; } @Override public void execute() { //do something someService.doTask(); } @Override public void setCron(String cronExpression) { this.cronExpression = cronExpression; } @Override public String getCron() { return cronExpression; } }

通过以上的定义。任务的时间和任务的逻辑可以根据不同的业务做到差异化配置。然后我们实现一个关于Task的FactoryBean。

public class TaskFactoryBean implements FactoryBean { private SomeService someService; private String cronExpression; @Override public Task getObject() throws Exception { CustomTask customTask = new CustomTask(someService); customTask.setCron(cronExpression); return customTask; } @Override public Class getObjectType() { return CustomTask.class; } @Override public boolean isSingleton() { return true; } public SomeService getSomeService() { return someService; } public void setSomeService(SomeService someService) { this.someService = someService; } public String getCron_Expression() { return cronExpression; } public void setCron_Expression(String cronExpression) { this.cronExpression = cronExpression; } }

3.3 FactoryBean 注入IoC

你可以使用xml的注入方式,当然也可以使用javaConfig的配置方式。这里我们使用javaConfig注入。我们将两个FactroyBean注入到Spring容器中去。

@Configuration public class Config { @Bean public TaskFactoryBean customTask() { TaskFactoryBean taskFactoryBean = new TaskFactoryBean(); taskFactoryBean.setCron_Expression("0 15 10 * * ?"); String word = "定时任务一"; SomeService someService = new SomeService(); someService.setWord(word); taskFactoryBean.setSomeService(someService); return taskFactoryBean; } @Bean public TaskFactoryBean otherTask() { TaskFactoryBean taskFactoryBean = new TaskFactoryBean(); taskFactoryBean.setCron_Expression("0 15 17 * * ?"); String word = "定时任务二"; SomeService someService = new SomeService(); someService.setWord(word); taskFactoryBean.setSomeService(someService); return taskFactoryBean; } }

3.4 FactoryBean的一些特点

一般如上声明后,@Bean注解如果不显式声明bean名称则方法名作为bean的名称,而且返回值作为注入的Bean。但是我们通过debug发现却是这样的:

That is, beans created by returning FactoryBeans with method names. So how do I return this FactoryBean? The answer is also given in the figure above by adding the reference "&" to the method. The specific reason can also be found in BeanFactory. It is really not that enemies do not gather together.

We tested the two beans declared above and also performed the business logic for different timed tasks excellently.

@Autowired private Task customTask; @Autowired private Task otherTask; @Test public void task() { customTask.execute(); otherTask.execute(); } At this point, I believe that everyone has a deeper understanding of "What is BeanFactory and FactoryBean in Java", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report