AbstractFactoryBean.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.springframework.beans.factory.config;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.beans.factory.FactoryBean;
  20. import org.springframework.beans.factory.InitializingBean;
  21. /**
  22.  * Simple template superclass for FactoryBean implementations thats allows
  23.  * for creating a singleton or a prototype, depending on a flag.
  24.  *
  25.  * <p>If the "singleton" flag is "true" (the default), this class will
  26.  * create once on initialization and subsequently return the singleton
  27.  * instance. Else, this class will create a new instance each time.
  28.  * Subclasses are responsible for implementing the abstract
  29.  * <code>createInstance</code> template method to actually create objects.
  30.  *
  31.  * @author Juergen Hoeller
  32.  * @author Keith Donald
  33.  * @since 25.04.2004
  34.  */
  35. public abstract class AbstractFactoryBean implements FactoryBean, InitializingBean {
  36. protected final Log logger = LogFactory.getLog(getClass());
  37. private boolean singleton = true;
  38. private Object singletonInstance;
  39. /**
  40.  * Set if a singleton should be created, or a new object
  41.  * on each request else. Default is true.
  42.  */
  43. public final void setSingleton(boolean singleton) {
  44. this.singleton = singleton;
  45. }
  46. public final boolean isSingleton() {
  47. return singleton;
  48. }
  49. public final void afterPropertiesSet() throws Exception {
  50. if (singletonInstance == null) {
  51. this.singletonInstance = createInstance();
  52. }
  53. }
  54. public final Object getObject() throws Exception {
  55. if (isSingleton()) {
  56. return this.singletonInstance;
  57. }
  58. else {
  59. return createInstance();
  60. }
  61. }
  62. /**
  63.  * Template method that subclasses must override to construct
  64.  * the object returned by this factory.
  65.  * <p>Invoked on initialization of this FactoryBean in case of
  66.  * a singleton; else, on each getObject() call.
  67.  * @return the object returned by this factory
  68.  * @throws Exception if an exception occured during object creation
  69.  * @see #getObject
  70.  */
  71. protected abstract Object createInstance() throws Exception;
  72. }