spring BeanFactory VS FactoryBean
一、FactoryBean示例
public class DateStringFactoryBean implements FactoryBean<Object> { private boolean isDate; public void setDate(boolean date) {
isDate = date;
} @Override
public Object getObject() {
return isDate ? new Date() : "2018-03-04";
} @Override
public Class<?> getObjectType() {
return isDate ? Date.class : String.class;
} @Override
public boolean isSingleton() {
return false;
}
}
AppConfig
public class AppConfig {
@Bean(name = "dateFactoryBean")
public DateStringFactoryBean createString(){
DateStringFactoryBean bean = new DateStringFactoryBean();
bean.setDate(true);
return bean;
} @Bean(name = "stringFactoryBean")
public DateStringFactoryBean createDate(){
DateStringFactoryBean bean = new DateStringFactoryBean();
bean.setDate(false);
return bean;
} }
Main
public class Main {
public static void main(String[] args) { AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("dateFactoryBean", Date.class));
System.out.println(context.getBean("stringFactoryBean", String.class)); System.out.println(context.getBean("&dateFactoryBean")); //使用&+beanName获得DateStringFactoryBean实例
System.out.println(context.getBean("&stringFactoryBean")); } }
二、调试分析
1. 系统启动时,会注册FactoryBean
2. context.getBean("dateFactoryBean", Date.class) 时
三、相关文档
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
- BeanNameAware's
setBeanName
- BeanClassLoaderAware's
setBeanClassLoader
- BeanFactoryAware's
setBeanFactory
- EnvironmentAware's
setEnvironment
- EmbeddedValueResolverAware's
setEmbeddedValueResolver
- ResourceLoaderAware's
setResourceLoader
(only applicable when running in an application context) - ApplicationEventPublisherAware's
setApplicationEventPublisher
(only applicable when running in an application context) - MessageSourceAware's
setMessageSource
(only applicable when running in an application context) - ApplicationContextAware's
setApplicationContext
(only applicable when running in an application context) - ServletContextAware's
setServletContext
(only applicable when running in a web application context) postProcessBeforeInitialization
methods of BeanPostProcessors- InitializingBean's
afterPropertiesSet
- a custom init-method definition
postProcessAfterInitialization
methods of BeanPostProcessors
BeanFactory 是 IOC 容器的编程抽象,比如 ApplicationContext, XmlBeanFactory 等,这些都是 IOC 容器的具体表现
FactoryBean 是一个可以在 IOC而容器中被管理的一个 bean,是对各种处理过程和资源使用的抽象,FactoryBean 在需要
时产生另一个对象,而不返回 FactoryBean本身,我们可以把它看成是一个抽象工厂,对它的调用返回的是工厂生产的产
品。Spring对代理对象的处理,对事务性代理的处理都使用了FactoryBean
public interface FactoryBean<T>
BeanFactory
which are themselves factories for individual objects.NB: A bean that implements this interface cannot be used as a normal bean. A FactoryBean is defined in a bean style,
but the object exposed for bean references (getObject()
) is always the object that it creates.
FactoryBeans can support singletons and prototypes, and can either create objects lazily on demand or eagerly on startup.
The SmartFactoryBean
interface allows for exposing more fine-grained behavioral metadata.
This interface is heavily used within the framework itself, for example for the AOP ProxyFactoryBean
.
It can be used for custom components as well; however, this is only common for infrastructure code.
FactoryBean
is a programmatic contract. Implementations are not supposed to rely on annotation-driven injection
or other reflective facilities. getObjectType()
getObject()
invocations may arrive early in the bootstrap process, even
ahead of any post-processor setup. If you need access other beans, implement BeanFactoryAware
and obtain them programmatically.
Finally, FactoryBean objects participate in the containing BeanFactory's synchronization of bean creation. There is usually no
need for internal synchronization other than for purposes of lazy initialization within the FactoryBean itself (or the like).
同类文章
spring BeanFactory VS FactoryBean的更多相关文章
- Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...
- Spring BeanFactory 与 FactoryBean 的区别
BeanFactory 和 FactoryBean 都是Spring Beans模块下的接口 BeanFactory是spring简单工厂模式的接口类,spring IOC特性核心类,提供从工厂类中获 ...
- BeanFactory 和FactoryBean的区别
转自:https://www.cnblogs.com/aspirant/p/9082858.html BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是 ...
- Difference between BeanFactory and FactoryBean in Spring Framework (Spring BeanFactory与Factory区别)
参见原文:http://www.geekabyte.io/2014/11/difference-between-beanfactory-and.html geekAbyte Codes and Ran ...
- Spring中BeanFactory与FactoryBean的区别
在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...
- Spring的BeanFactory和FactoryBean
官方定义 BeanFactory:Spring Bean容器的根接口 FactoryBean:各个对象的工厂接口,如果bean实现了这个接口,它将被用作对象的工厂,而不是直接作为bean实例. 源码解 ...
- spring:Beanfactory和ApplicationContext、BeanFactory 和 FactoryBean
1.Beanfactory和ApplicationContext有什么区别 ApplicationContext (1)在配置文件加载后创建bean 利用debug方式,在Student类的无参构造方 ...
- Spring中的BeanFactory与FactoryBean看这一篇就够了
前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...
- Spring中BeanFactory与FactoryBean到底有什么区别?
一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层规范,是SpringIoc容器的核心接口,它定义了getBean().containsBean()等管理Bea ...
随机推荐
- Effective Java 第三版——88. 防御性地编写READOBJECT方法
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略、价值领先战略
Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略.价值领先战略 美国著名管理学家迈克尔•波特在<竞争战略>一书中提出了集中化战略和差异化战略.成本领先战略三种基本竞争战略,从 ...
- R语言 ggplot2包
R语言 ggplot2包的学习 分析数据要做的第一件事情,就是观察它.对于每个变量,哪些值是最常见的?值域是大是小?是否有异常观测? ggplot2图形之基本语法: ggplot2的核心理念是将 ...
- caffe网络结构可视化在线工具
http://ethereon.github.io/netscope/#/editor shift+enter
- SQL查看死锁+清理死锁
----查看sql死锁 CREATE procedure sp_who_lock as begin declare @spid int declare ...
- log4net 如何关闭Nhibernate产生的大量日志
[参考文献]Log4Net指南 非常完善的Log4net详细说明 C# 使用Log4Net记录日志(进阶篇) 此问题困扰良久 良久 良久 appender filter ,show_sql false ...
- Spring Security的几个重要词
1.SecurityContextHolder:是安全上下文容器,可以在此得知操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保存在SecurityContextHolder中. O ...
- MySQL 数据热备份
mysqlhotcopy使用lock tables.flush tables和cp或scp来快速备份数据库.它是备份数据库或单个表最快的途径,完全属于物理备份,但只能运行在数据库目录所在的机器上. 与 ...
- JavaScript 事件绑定函数
function panTest(m_onClickFun) { var This = this; This.onClickFun = m_onClickFun; /* This.onClickFun ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...