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.context.expression; 018 019import org.springframework.beans.BeansException; 020import org.springframework.beans.factory.BeanFactory; 021import org.springframework.expression.AccessException; 022import org.springframework.expression.BeanResolver; 023import org.springframework.expression.EvaluationContext; 024import org.springframework.util.Assert; 025 026/** 027 * EL bean resolver that operates against a Spring 028 * {@link org.springframework.beans.factory.BeanFactory}. 029 * 030 * @author Juergen Hoeller 031 * @since 3.0.4 032 */ 033public class BeanFactoryResolver implements BeanResolver { 034 035 private final BeanFactory beanFactory; 036 037 038 /** 039 * Create a new {@link BeanFactoryResolver} for the given factory. 040 * @param beanFactory the {@link BeanFactory} to resolve bean names against 041 */ 042 public BeanFactoryResolver(BeanFactory beanFactory) { 043 Assert.notNull(beanFactory, "BeanFactory must not be null"); 044 this.beanFactory = beanFactory; 045 } 046 047 048 @Override 049 public Object resolve(EvaluationContext context, String beanName) throws AccessException { 050 try { 051 return this.beanFactory.getBean(beanName); 052 } 053 catch (BeansException ex) { 054 throw new AccessException("Could not resolve bean reference against BeanFactory", ex); 055 } 056 } 057 058}