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.util.Assert;
020:
021: /**
022: * Immutable placeholder class used for a property value object when it's
023: * a reference to another bean in the factory, to be resolved at runtime.
024: *
025: * @author Rod Johnson
026: * @author Juergen Hoeller
027: * @see BeanDefinition#getPropertyValues()
028: * @see org.springframework.beans.factory.BeanFactory#getBean
029: */
030: public class RuntimeBeanReference implements BeanReference {
031:
032: private final String beanName;
033:
034: private final boolean toParent;
035:
036: private Object source;
037:
038: /**
039: * Create a new RuntimeBeanReference to the given bean name,
040: * without explicitly marking it as reference to a bean in
041: * the parent factory.
042: * @param beanName name of the target bean
043: */
044: public RuntimeBeanReference(String beanName) {
045: this (beanName, false);
046: }
047:
048: /**
049: * Create a new RuntimeBeanReference to the given bean name,
050: * with the option to mark it as reference to a bean in
051: * the parent factory.
052: * @param beanName name of the target bean
053: * @param toParent whether this is an explicit reference to
054: * a bean in the parent factory
055: */
056: public RuntimeBeanReference(String beanName, boolean toParent) {
057: Assert.hasText(beanName, "'beanName' must not be empty");
058: this .beanName = beanName;
059: this .toParent = toParent;
060: }
061:
062: public String getBeanName() {
063: return this .beanName;
064: }
065:
066: /**
067: * Return whether this is an explicit reference to a bean
068: * in the parent factory.
069: */
070: public boolean isToParent() {
071: return this .toParent;
072: }
073:
074: /**
075: * Set the configuration source <code>Object</code> for this metadata element.
076: * <p>The exact type of the object will depend on the configuration mechanism used.
077: */
078: public void setSource(Object source) {
079: this .source = source;
080: }
081:
082: public Object getSource() {
083: return this .source;
084: }
085:
086: public boolean equals(Object other) {
087: if (this == other) {
088: return true;
089: }
090: if (!(other instanceof RuntimeBeanReference)) {
091: return false;
092: }
093: RuntimeBeanReference that = (RuntimeBeanReference) other;
094: return (this .beanName.equals(that.beanName) && this .toParent == that.toParent);
095: }
096:
097: public int hashCode() {
098: int result = this .beanName.hashCode();
099: result = 29 * result + (this .toParent ? 1 : 0);
100: return result;
101: }
102:
103: public String toString() {
104: return '<' + getBeanName() + '>';
105: }
106:
107: }
|