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