001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.beans.factory.config; 018 019import org.springframework.util.Assert; 020 021/** 022 * Immutable placeholder class used for a property value object when it's a 023 * reference to another bean name in the factory, to be resolved at runtime. 024 * 025 * @author Juergen Hoeller 026 * @since 2.0 027 * @see RuntimeBeanReference 028 * @see BeanDefinition#getPropertyValues() 029 * @see org.springframework.beans.factory.BeanFactory#getBean 030 */ 031public class RuntimeBeanNameReference implements BeanReference { 032 033 private final String beanName; 034 035 private Object source; 036 037 038 /** 039 * Create a new RuntimeBeanNameReference to the given bean name. 040 * @param beanName name of the target bean 041 */ 042 public RuntimeBeanNameReference(String beanName) { 043 Assert.hasText(beanName, "'beanName' must not be empty"); 044 this.beanName = beanName; 045 } 046 047 @Override 048 public String getBeanName() { 049 return this.beanName; 050 } 051 052 /** 053 * Set the configuration source {@code Object} for this metadata element. 054 * <p>The exact type of the object will depend on the configuration mechanism used. 055 */ 056 public void setSource(Object source) { 057 this.source = source; 058 } 059 060 @Override 061 public Object getSource() { 062 return this.source; 063 } 064 065 066 @Override 067 public boolean equals(Object other) { 068 if (this == other) { 069 return true; 070 } 071 if (!(other instanceof RuntimeBeanNameReference)) { 072 return false; 073 } 074 RuntimeBeanNameReference that = (RuntimeBeanNameReference) other; 075 return this.beanName.equals(that.beanName); 076 } 077 078 @Override 079 public int hashCode() { 080 return this.beanName.hashCode(); 081 } 082 083 @Override 084 public String toString() { 085 return '<' + getBeanName() + '>'; 086 } 087 088}