001 /* 002 * Copyright 2002-2004 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 * http://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 017 package org.springframework.beans.factory.support; 018 019 import org.apache.commons.logging.Log; 020 import org.apache.commons.logging.LogFactory; 021 022 import org.springframework.beans.BeansException; 023 import org.springframework.core.io.DefaultResourceLoader; 024 import org.springframework.core.io.Resource; 025 import org.springframework.core.io.ResourceLoader; 026 027 /** 028 * Abstract base class for bean definition readers which implement 029 * the BeanDefinitionReader interface. 030 * 031 * <p>Provides common properties like the bean factory to work on 032 * and the class loader to use for loading bean classes. 033 * 034 * @author Juergen Hoeller 035 * @since 11.12.2003 036 * @see BeanDefinitionReaderUtils 037 */ 038 public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader { 039 040 protected final Log logger = LogFactory.getLog(getClass()); 041 042 private final BeanDefinitionRegistry beanFactory; 043 Rate044 private ClassLoader beanClassLoader = Thread.currentThread().getContextClassLoader(); 045 046 private ResourceLoader resourceLoader = new DefaultResourceLoader(); 047 048 049 /** 050 * Create a new AbstractBeanDefinitionReader for the given factory. 051 * @param beanFactory the bean factory to work on 052 */ 053 protected AbstractBeanDefinitionReader(BeanDefinitionRegistry beanFactory) { 054 this.beanFactory = beanFactory; 055 } 056 057 public BeanDefinitionRegistry getBeanFactory() { 058 return beanFactory; 059 } 060 061 /** 062 * Set the ClassLoader to use for bean classes. 063 * Default is the thread context class loader. 064 * <p>Setting this to null suggests to not load bean classes but just register 065 * bean definitions with class names, for example when just registering beans 066 * in a registry but not actually instantiating them in a factory. 067 * @see java.lang.Thread#getContextClassLoader 068 */ 069 public void setBeanClassLoader(ClassLoader beanClassLoader) { 070 this.beanClassLoader = beanClassLoader; 071 } 072 073 public ClassLoader getBeanClassLoader() { 074 return beanClassLoader; 075 } 076 077 /** 078 * Set the ResourceLoader to use for resource locations. 079 * Default is DefaultResourceLoader. 080 * <p>Can also be a ResourcePatternResolver, additionally capable 081 * of resolving resource patterns to Resource arrays. 082 * <p>Setting this to null suggests that absolute resource loading 083 * is not available for this bean definition reader. 084 * @see org.springframework.core.io.DefaultResourceLoader 085 * @see org.springframework.core.io.support.ResourcePatternResolver 086 */ 087 public void setResourceLoader(ResourceLoader resourceLoader) { 088 this.resourceLoader = resourceLoader; 089 } 090 091 public ResourceLoader getResourceLoader() { 092 return resourceLoader; 093 } 094 095 096 public int loadBeanDefinitions(Resource[] resources) throws BeansException { 097 int counter = 0; 098 for (int i = 0; i < resources.length; i++) { 099 counter += loadBeanDefinitions(resources[i]); 100 } 101 return counter; 102 } 103 104 }