一、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:

  1. BeanNameAware's setBeanName
  2. BeanClassLoaderAware's setBeanClassLoader
  3. BeanFactoryAware's setBeanFactory
  4. EnvironmentAware's setEnvironment
  5. EmbeddedValueResolverAware's setEmbeddedValueResolver
  6. ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)
  7. ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)
  8. MessageSourceAware's setMessageSource (only applicable when running in an application context)
  9. ApplicationContextAware's setApplicationContext (only applicable when running in an application context)
  10. ServletContextAware's setServletContext (only applicable when running in a web application context)
  11. postProcessBeforeInitialization methods of BeanPostProcessors
  12. InitializingBean's afterPropertiesSet
  13. a custom init-method definition
  14. postProcessAfterInitialization methods of BeanPostProcessors

BeanFactory 是 IOC 容器的编程抽象,比如 ApplicationContext, XmlBeanFactory 等,这些都是 IOC 容器的具体表现

FactoryBean 是一个可以在 IOC而容器中被管理的一个 bean,是对各种处理过程和资源使用的抽象,FactoryBean 在需要

时产生另一个对象,而不返回 FactoryBean本身,我们可以把它看成是一个抽象工厂,对它的调用返回的是工厂生产的产

品。Spring对代理对象的处理,对事务性代理的处理都使用了FactoryBean

public interface FactoryBean<T>
Interface to be implemented by objects used within a BeanFactory which are themselves factories for individual objects.
If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.

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 aop对象生成

最新文章

  1. [z]Oracle性能优化-读懂执行计划
  2. 套题 codeforces 360
  3. tp生成验证码
  4. C/C++函数使用
  5. java中的this与super的区别
  6. 每天一个linux命令-转载
  7. Connect to a Windows PC from Ubuntu via Remote Desktop Connection
  8. 命令行dump anr traces.txt文件
  9. HDU 4706 Children&#39;s Day(简单模拟)
  10. C++顺序性容器、关联性容器与容器适配器
  11. BZOJ 1355 Baltic2009 Radio Transmission KMP算法
  12. Masonry 在swift下的使用
  13. NB-IoT物联网,来了
  14. 搜索引擎之全文搜索算法功能实现(基于Lucene)
  15. Python一些代码
  16. mui中confirm在苹果出现bug,confirm点击确定跳转页面再返回后,页面被遮罩盖住无法使用
  17. metasploit中meterpreter命令
  18. bootstrap----屏幕大小切换
  19. IdentityServer4目录
  20. MySQL 安全整理

热门文章

  1. logstash之filter处理中括号包围的内容
  2. VBA二次学习笔记(3)——批量合并单元格
  3. session多服务器共享的方案梳理
  4. tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed(转)
  5. test001
  6. AI人工智能顶级实战工程师 课程大纲
  7. laravel5.8笔记六:公共函数和常量设置
  8. 【LInux】统计某文件夹下目录的个数
  9. java直接生成zip压缩文件精简代码(跳过txt文件)
  10. C# SQLite数据库