KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > DummyFactory


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.beans.factory;
18
19 import org.springframework.beans.BeansException;
20 import org.springframework.beans.TestBean;
21 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
22
23 /**
24  * Simple factory to allow testing of FactoryBean support in AbstractBeanFactory.
25  * Depending on whether its singleton property is set, it will return a singleton
26  * or a prototype instance.
27  *
28  * <p>Implements InitializingBean interface, so we can check that
29  * factories get this lifecycle callback if they want.
30  *
31  * @author Rod Johnson
32  * @since 10.03.2003
33  */

34 public class DummyFactory
35         implements FactoryBean, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
36     
37     public static final String JavaDoc SINGLETON_NAME = "Factory singleton";
38
39     private static boolean prototypeCreated;
40
41     /**
42      * Clear static state.
43      */

44     public static void reset() {
45         prototypeCreated = false;
46     }
47
48
49     /**
50      * Default is for factories to return a singleton instance.
51      */

52     private boolean singleton = true;
53
54     private String JavaDoc beanName;
55
56     private AutowireCapableBeanFactory beanFactory;
57
58     private boolean postProcessed;
59
60     private boolean initialized;
61
62     private TestBean testBean;
63
64     private TestBean otherTestBean;
65
66
67     public DummyFactory() {
68         this.testBean = new TestBean();
69         this.testBean.setName(SINGLETON_NAME);
70         this.testBean.setAge(25);
71     }
72
73     /**
74      * Return if the bean managed by this factory is a singleton.
75      * @see org.springframework.beans.factory.FactoryBean#isSingleton()
76      */

77     public boolean isSingleton() {
78         return this.singleton;
79     }
80
81     /**
82      * Set if the bean managed by this factory is a singleton.
83      */

84     public void setSingleton(boolean singleton) {
85         this.singleton = singleton;
86     }
87
88     public void setBeanName(String JavaDoc beanName) {
89         this.beanName = beanName;
90     }
91
92     public String JavaDoc getBeanName() {
93         return beanName;
94     }
95
96     public void setBeanFactory(BeanFactory beanFactory) {
97         this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
98         this.beanFactory.applyBeanPostProcessorsBeforeInitialization(this.testBean, this.beanName);
99     }
100
101     public BeanFactory getBeanFactory() {
102         return beanFactory;
103     }
104
105     public void setPostProcessed(boolean postProcessed) {
106         this.postProcessed = postProcessed;
107     }
108
109     public boolean isPostProcessed() {
110         return postProcessed;
111     }
112
113     public void setOtherTestBean(TestBean otherTestBean) {
114         this.otherTestBean = otherTestBean;
115     }
116
117     public TestBean getOtherTestBean() {
118         return otherTestBean;
119     }
120
121     public void afterPropertiesSet() {
122         if (initialized) {
123             throw new RuntimeException JavaDoc("Cannot call afterPropertiesSet twice on the one bean");
124         }
125         this.initialized = true;
126     }
127     
128     /**
129      * Was this initialized by invocation of the
130      * afterPropertiesSet() method from the InitializingBean interface?
131      */

132     public boolean wasInitialized() {
133         return initialized;
134     }
135
136     public static boolean wasPrototypeCreated() {
137         return prototypeCreated;
138     }
139
140
141     /**
142      * Return the managed object, supporting both singleton
143      * and prototype mode.
144      * @see org.springframework.beans.factory.FactoryBean#getObject()
145      */

146     public Object JavaDoc getObject() throws BeansException {
147         if (isSingleton()) {
148             return this.testBean;
149         }
150         else {
151             TestBean prototype = new TestBean("prototype created at " + System.currentTimeMillis(), 11);
152             if (this.beanFactory != null) {
153                 this.beanFactory.applyBeanPostProcessorsBeforeInitialization(prototype, this.beanName);
154             }
155             prototypeCreated = true;
156             return prototype;
157         }
158     }
159
160     public Class JavaDoc getObjectType() {
161         return TestBean.class;
162     }
163
164
165     public void destroy() {
166         if (this.testBean != null) {
167             this.testBean.setName(null);
168         }
169     }
170
171 }
172
Popular Tags