001/* 002 * Copyright 2002-2014 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.factory.BeanFactory; 020import org.springframework.expression.AccessException; 021import org.springframework.expression.EvaluationContext; 022import org.springframework.expression.PropertyAccessor; 023import org.springframework.expression.TypedValue; 024 025/** 026 * EL property accessor that knows how to traverse the beans of a 027 * Spring {@link org.springframework.beans.factory.BeanFactory}. 028 * 029 * @author Juergen Hoeller 030 * @author Andy Clement 031 * @since 3.0 032 */ 033public class BeanFactoryAccessor implements PropertyAccessor { 034 035 @Override 036 public Class<?>[] getSpecificTargetClasses() { 037 return new Class<?>[] {BeanFactory.class}; 038 } 039 040 @Override 041 public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException { 042 return (((BeanFactory) target).containsBean(name)); 043 } 044 045 @Override 046 public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { 047 return new TypedValue(((BeanFactory) target).getBean(name)); 048 } 049 050 @Override 051 public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { 052 return false; 053 } 054 055 @Override 056 public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { 057 throw new AccessException("Beans in a BeanFactory are read-only"); 058 } 059 060}