KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > BeanReferenceFactoryBean


1 /*
2  * Copyright 2002-2007 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.config;
18
19 import org.springframework.beans.BeansException;
20 import org.springframework.beans.factory.BeanFactory;
21 import org.springframework.beans.factory.BeanFactoryAware;
22 import org.springframework.beans.factory.FactoryBeanNotInitializedException;
23 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24 import org.springframework.beans.factory.SmartFactoryBean;
25
26 /**
27  * FactoryBean that exposes an arbitrary target bean under a different name.
28  *
29  * <p>Usually, the target bean will reside in a different bean definition file,
30  * using this FactoryBean to link it in and expose it under a different name.
31  * Effectively, this corresponds to an alias for the target bean.
32  *
33  * <p><b>NOTE:</b> For XML bean definition files, an <code>&lt;alias&gt;</code>
34  * tag is available that effectively achieves the same.
35  *
36  * <p>A special capability of this FactoryBean is enabled through its configuration
37  * as bean definition: The "targetBeanName" can be substituted through a placeholder,
38  * in combination with Spring's {@link PropertyPlaceholderConfigurer}.
39  * Thanks to Marcus Bristav for pointing this out!
40  *
41  * @author Juergen Hoeller
42  * @since 1.2
43  * @see #setTargetBeanName
44  * @see PropertyPlaceholderConfigurer
45  */

46 public class BeanReferenceFactoryBean implements SmartFactoryBean, BeanFactoryAware {
47
48     private String JavaDoc targetBeanName;
49
50     private BeanFactory beanFactory;
51
52
53     /**
54      * Set the name of the target bean.
55      * <p>This property is required. The value for this property can be
56      * substituted through a placeholder, in combination with Spring's
57      * PropertyPlaceholderConfigurer.
58      * @param targetBeanName the name of the target bean
59      * @see PropertyPlaceholderConfigurer
60      */

61     public void setTargetBeanName(String JavaDoc targetBeanName) {
62         this.targetBeanName = targetBeanName;
63     }
64
65     public void setBeanFactory(BeanFactory beanFactory) {
66         this.beanFactory = beanFactory;
67         if (this.targetBeanName == null) {
68             throw new IllegalArgumentException JavaDoc("'targetBeanName' is required");
69         }
70         if (!this.beanFactory.containsBean(this.targetBeanName)) {
71             throw new NoSuchBeanDefinitionException(this.targetBeanName, this.beanFactory.toString());
72         }
73     }
74
75
76     public Object JavaDoc getObject() throws BeansException {
77         if (this.beanFactory == null) {
78             throw new FactoryBeanNotInitializedException();
79         }
80         return this.beanFactory.getBean(this.targetBeanName);
81     }
82
83     public Class JavaDoc getObjectType() {
84         return this.beanFactory.getType(this.targetBeanName);
85     }
86
87     public boolean isSingleton() {
88         return this.beanFactory.isSingleton(this.targetBeanName);
89     }
90
91     public boolean isPrototype() {
92         return this.beanFactory.isPrototype(this.targetBeanName);
93     }
94
95 }
96
Popular Tags