1 16 17 package org.springframework.beans.factory.support; 18 19 import java.io.IOException ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import org.springframework.beans.factory.BeanDefinitionStoreException; 25 import org.springframework.core.io.Resource; 26 import org.springframework.core.io.ResourceLoader; 27 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 28 import org.springframework.core.io.support.ResourcePatternResolver; 29 import org.springframework.util.Assert; 30 31 42 public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader { 43 44 protected final Log logger = LogFactory.getLog(getClass()); 45 46 private final BeanDefinitionRegistry beanFactory; 47 48 private BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); 49 50 private ResourceLoader resourceLoader; 51 52 private ClassLoader beanClassLoader; 53 54 55 67 protected AbstractBeanDefinitionReader(BeanDefinitionRegistry beanFactory) { 68 Assert.notNull(beanFactory, "Bean factory must not be null"); 69 this.beanFactory = beanFactory; 70 71 if (this.beanFactory instanceof ResourceLoader) { 73 this.resourceLoader = (ResourceLoader) this.beanFactory; 74 } 75 else { 76 this.resourceLoader = new PathMatchingResourcePatternResolver(); 77 } 78 } 79 80 public BeanDefinitionRegistry getBeanFactory() { 81 return this.beanFactory; 82 } 83 84 public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) { 85 this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new DefaultBeanNameGenerator()); 86 } 87 88 public BeanNameGenerator getBeanNameGenerator() { 89 return this.beanNameGenerator; 90 } 91 92 103 public void setResourceLoader(ResourceLoader resourceLoader) { 104 this.resourceLoader = resourceLoader; 105 } 106 107 public ResourceLoader getResourceLoader() { 108 return this.resourceLoader; 109 } 110 111 118 public void setBeanClassLoader(ClassLoader beanClassLoader) { 119 this.beanClassLoader = beanClassLoader; 120 } 121 122 public ClassLoader getBeanClassLoader() { 123 return this.beanClassLoader; 124 } 125 126 127 public int loadBeanDefinitions(Resource[] resources) throws BeanDefinitionStoreException { 128 Assert.notNull(resources, "Resource array must not be null"); 129 int counter = 0; 130 for (int i = 0; i < resources.length; i++) { 131 counter += loadBeanDefinitions(resources[i]); 132 } 133 return counter; 134 } 135 136 public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException { 137 ResourceLoader resourceLoader = getResourceLoader(); 138 if (resourceLoader == null) { 139 throw new BeanDefinitionStoreException( 140 "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available"); 141 } 142 143 if (resourceLoader instanceof ResourcePatternResolver) { 144 try { 146 Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location); 147 int loadCount = loadBeanDefinitions(resources); 148 if (logger.isDebugEnabled()) { 149 logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]"); 150 } 151 return loadCount; 152 } 153 catch (IOException ex) { 154 throw new BeanDefinitionStoreException( 155 "Could not resolve bean definition resource pattern [" + location + "]", ex); 156 } 157 } 158 else { 159 Resource resource = resourceLoader.getResource(location); 161 int loadCount = loadBeanDefinitions(resource); 162 if (logger.isDebugEnabled()) { 163 logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]"); 164 } 165 return loadCount; 166 } 167 } 168 169 public int loadBeanDefinitions(String [] locations) throws BeanDefinitionStoreException { 170 Assert.notNull(locations, "Location array must not be null"); 171 int counter = 0; 172 for (int i = 0; i < locations.length; i++) { 173 counter += loadBeanDefinitions(locations[i]); 174 } 175 return counter; 176 } 177 178 } 179 | Popular Tags |