001/* 002 * Copyright 2002-2018 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.lang.Nullable; 020import org.springframework.util.Assert; 021 022/** 023 * Context object for evaluating an expression within a bean definition. 024 * 025 * @author Juergen Hoeller 026 * @since 3.0 027 */ 028public class BeanExpressionContext { 029 030 private final ConfigurableBeanFactory beanFactory; 031 032 @Nullable 033 private final Scope scope; 034 035 036 public BeanExpressionContext(ConfigurableBeanFactory beanFactory, @Nullable Scope scope) { 037 Assert.notNull(beanFactory, "BeanFactory must not be null"); 038 this.beanFactory = beanFactory; 039 this.scope = scope; 040 } 041 042 public final ConfigurableBeanFactory getBeanFactory() { 043 return this.beanFactory; 044 } 045 046 @Nullable 047 public final Scope getScope() { 048 return this.scope; 049 } 050 051 052 public boolean containsObject(String key) { 053 return (this.beanFactory.containsBean(key) || 054 (this.scope != null && this.scope.resolveContextualObject(key) != null)); 055 } 056 057 @Nullable 058 public Object getObject(String key) { 059 if (this.beanFactory.containsBean(key)) { 060 return this.beanFactory.getBean(key); 061 } 062 else if (this.scope != null) { 063 return this.scope.resolveContextualObject(key); 064 } 065 else { 066 return null; 067 } 068 } 069 070 071 @Override 072 public boolean equals(@Nullable Object other) { 073 if (this == other) { 074 return true; 075 } 076 if (!(other instanceof BeanExpressionContext)) { 077 return false; 078 } 079 BeanExpressionContext otherContext = (BeanExpressionContext) other; 080 return (this.beanFactory == otherContext.beanFactory && this.scope == otherContext.scope); 081 } 082 083 @Override 084 public int hashCode() { 085 return this.beanFactory.hashCode(); 086 } 087 088}