001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import org.springframework.beans.BeansException;
020: import org.springframework.beans.factory.BeanFactory;
021: import org.springframework.beans.factory.BeanFactoryAware;
022: import org.springframework.beans.factory.FactoryBeanNotInitializedException;
023: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
024: import org.springframework.beans.factory.SmartFactoryBean;
025:
026: /**
027: * FactoryBean that exposes an arbitrary target bean under a different name.
028: *
029: * <p>Usually, the target bean will reside in a different bean definition file,
030: * using this FactoryBean to link it in and expose it under a different name.
031: * Effectively, this corresponds to an alias for the target bean.
032: *
033: * <p><b>NOTE:</b> For XML bean definition files, an <code><alias></code>
034: * tag is available that effectively achieves the same.
035: *
036: * <p>A special capability of this FactoryBean is enabled through its configuration
037: * as bean definition: The "targetBeanName" can be substituted through a placeholder,
038: * in combination with Spring's {@link PropertyPlaceholderConfigurer}.
039: * Thanks to Marcus Bristav for pointing this out!
040: *
041: * @author Juergen Hoeller
042: * @since 1.2
043: * @see #setTargetBeanName
044: * @see PropertyPlaceholderConfigurer
045: */
046: public class BeanReferenceFactoryBean implements SmartFactoryBean,
047: BeanFactoryAware {
048:
049: private String targetBeanName;
050:
051: private BeanFactory beanFactory;
052:
053: /**
054: * Set the name of the target bean.
055: * <p>This property is required. The value for this property can be
056: * substituted through a placeholder, in combination with Spring's
057: * PropertyPlaceholderConfigurer.
058: * @param targetBeanName the name of the target bean
059: * @see PropertyPlaceholderConfigurer
060: */
061: public void setTargetBeanName(String targetBeanName) {
062: this .targetBeanName = targetBeanName;
063: }
064:
065: public void setBeanFactory(BeanFactory beanFactory) {
066: this .beanFactory = beanFactory;
067: if (this .targetBeanName == null) {
068: throw new IllegalArgumentException(
069: "'targetBeanName' is required");
070: }
071: if (!this .beanFactory.containsBean(this .targetBeanName)) {
072: throw new NoSuchBeanDefinitionException(
073: this .targetBeanName, this .beanFactory.toString());
074: }
075: }
076:
077: public Object getObject() throws BeansException {
078: if (this .beanFactory == null) {
079: throw new FactoryBeanNotInitializedException();
080: }
081: return this .beanFactory.getBean(this .targetBeanName);
082: }
083:
084: public Class getObjectType() {
085: if (this .beanFactory == null) {
086: return null;
087: }
088: return this .beanFactory.getType(this .targetBeanName);
089: }
090:
091: public boolean isSingleton() {
092: if (this .beanFactory == null) {
093: throw new FactoryBeanNotInitializedException();
094: }
095: return this .beanFactory.isSingleton(this .targetBeanName);
096: }
097:
098: public boolean isPrototype() {
099: if (this .beanFactory == null) {
100: throw new FactoryBeanNotInitializedException();
101: }
102: return this .beanFactory.isPrototype(this .targetBeanName);
103: }
104:
105: public boolean isEagerInit() {
106: return false;
107: }
108:
109: }
|