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