1 22 23 package org.jboss.spring.factory; 24 25 import java.io.IOException ; 26 27 import org.springframework.beans.BeansException; 28 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 29 import org.springframework.beans.factory.xml.ResourceEntityResolver; 30 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 31 import org.springframework.context.support.ClassPathXmlApplicationContext; 32 import org.springframework.core.io.Resource; 33 34 37 public class NamedXmlApplicationContext extends ClassPathXmlApplicationContext 38 implements Nameable 39 { 40 41 private String defaultName; 42 private Resource resource; 43 private NamedXmlBeanDefinitionReader beanDefinitionReader; 44 45 48 public NamedXmlApplicationContext(String defaultName, Resource resource) throws BeansException 49 { 50 this(defaultName, resource, true); 51 } 52 53 public NamedXmlApplicationContext(String defaultName, Resource resource, boolean refresh) throws BeansException 54 { 55 super(new String []{}, false); 57 this.defaultName = defaultName; 58 this.resource = resource; 59 if (refresh) 60 { 61 refresh(); 62 } 63 } 64 65 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException 66 { 67 beanDefinitionReader = new NamedXmlBeanDefinitionReader(beanFactory); 69 70 beanDefinitionReader.setResourceLoader(this); 73 if (getClassLoader() != null) 74 { 75 beanDefinitionReader.setBeanClassLoader(getClassLoader()); 76 } 77 beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); 78 79 initBeanDefinitionReader(beanDefinitionReader); 82 loadBeanDefinitions(beanDefinitionReader); 83 } 84 85 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException 86 { 87 reader.loadBeanDefinitions(resource); 88 } 89 90 public String getName() 91 { 92 String name = beanDefinitionReader.getName() != null ? beanDefinitionReader.getName() : defaultName; 93 if (name == null) 94 { 95 throw new IllegalArgumentException ("Bean factory JNDI name must be set!"); 96 } 97 return name; 98 } 99 100 } 101 | Popular Tags |