怎么用工厂Bean让春天配置动起来

介绍

这篇文章主要介绍”怎么用工厂Bean让春天配置动起来”,在日常操作中,相信很多人在怎么用工厂Bean让春天配置动起来问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答“怎么用工厂Bean让春天配置动起来”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

不少朋友讨论春天配置时认为春天配置中只能静态的设置一些参数(典型情况如数据库配置,定时器配置等)导致不方便,其实春天已经提供了非常便利的方式来实现动态春天配置,我们要做的只是实现一个自己的工厂Bean来看一下工厂Bean接口的定义

<李>

/* *//* *,

<李>

*, Interface 用be  implemented  by  objects  used  within  a  BeanFactory,

<李>

*, that 断开连接;themselves 工厂只If  a  bean  implements 却;能够界面,,

<李>

*, it  is  used  as  a 工厂,not  directly  as  a  Bean。李,

<>李*,

<李>

*,

注:,A  bean  that  implements 却;能够interface  cannot  be 使用,

<李>

*, as  A  normal  bean。, A  FactoryBean  is  defined 拷贝A  bean 风格,,

<李>

*, but 从而object  exposed  for  bean  references  is  always 从而对象,

<李>

*, that  it 创建。,,

<李>

*,

FactoryBeans 还要support  singletons 以及原型,以及可以,

<李>

*, either  create  objects  lazily 提醒demand 或是eagerly 提醒启动。李,

<>李*,

<李>

*,

却;能够interface  is  heavily  used  within 从而框架,,,,

<李>

*, example  for 从而AOP  ProxyFactoryBean 一、JndiObjectFactoryBean。李,

<李>

*, It 还要be  used  for  application 组件,but 却;能够is  not 常见,

<李>

*, outside  of  infrastructure 代码。李,

<>李*,

<李>

*, @author  Rod 约翰逊,

<李>

*, @author  Juergen  hoel,

<李>

*, @since  08.03.2003,

<李>

*, @see  org.springframework.beans.factory。李BeanFactory,

<李>

*, @see  org.springframework.aop.framework.ProxyFactoryBean,

<李>

*, @see  org.springframework.jndi。李JndiObjectFactoryBean,

<李>

*/,

<李>

public  interface  FactoryBean …{,李,

<李>

/* *//* *,

<李>

*, Return  an  instance  (possibly  shared 或是独立),of 从而对象,

<李>

*, managed  by 却;能够工厂只As  with  a  BeanFactory,,却,能够允许,

<李>

*, support  for  both 从而Singleton 以及Prototype 图纸设计模式。李,

<李>

*,

If 却;能够method  returns  <代码>零代码> <李>

*,从而FactoryBean  as  not  fully  initialized 以及throw  a 对应,

<李>

*, FactoryBeanNotInitializedException。李,

<李>

*, @return  an  instance  of 从而bean  (should  not  be  <代码>零> <李>

*, a 零,<代码> value  will  be  considered  as  an  indication 的,

<李>

*, incomplete 初始化),

<李>

*, @throws  Exception 拷贝case  of  creation 错误,

<李>

*, @see  FactoryBeanNotInitializedException,

<李>

*/,

<李>

Object  getObject (), throws 例外;,李,

<李>

/* *//* *,

<李>

*, Return 从而type  of  object  that 却;能够FactoryBean 创建,,或是零,<代码>

<李>

*, if  not  known 推进只拷贝却;能够allows 用check  for  specific 类型,

<李>

*, of  beans  without  instantiating 对象,for  example 提醒自动装配。李,

<李>

*,

For  a  singleton,,却;能够should  try 用avoid  singleton 创建,

<李>

*, as  far  as ;, it  should  rather  estimate 从而type 拷贝。李,

<李>

怎么用工厂Bean让春天配置动起来