1 16 17 package org.springframework.beans.factory.access; 18 19 import org.springframework.beans.BeansException; 20 import org.springframework.beans.factory.BeanFactory; 21 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 22 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 23 import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; 24 import org.springframework.util.Assert; 25 26 46 public class BeanFactoryBootstrap { 47 48 public static final String BEAN_FACTORY_BEAN_NAME = "bootstrapBeanFactory"; 49 50 private static BeanFactoryBootstrap instance; 51 52 private static BeansException startupException; 53 54 private static void initializeSingleton() { 55 try { 56 instance = new BeanFactoryBootstrap(); 57 } 58 catch (BeansException ex) { 59 startupException = ex; 60 } 61 } 62 63 static { 66 initializeSingleton(); 67 } 68 69 74 public static BeanFactoryBootstrap getInstance() throws BeansException { 75 if (startupException != null) { 76 throw startupException; 77 } 78 Assert.notNull(instance); 79 return instance; 80 } 81 82 86 protected static void reinitialize() { 87 instance = null; 88 startupException = null; 89 initializeSingleton(); 90 } 91 92 93 94 private BeanFactory bootstrapFactory; 95 96 99 private BeanFactoryBootstrap() throws BeansException { 100 DefaultListableBeanFactory startupFactory = new DefaultListableBeanFactory(); 101 PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(startupFactory); 102 try { 103 propReader.registerBeanDefinitions(System.getProperties()); 104 this.bootstrapFactory = (BeanFactory) startupFactory.getBean(BEAN_FACTORY_BEAN_NAME, BeanFactory.class); 105 } 106 catch (NoSuchBeanDefinitionException ex) { 107 throw new BootstrapException( 108 "No bean named '" + BEAN_FACTORY_BEAN_NAME + "' in system properties: [" + startupFactory + "]"); 109 } 110 catch (BeansException ex) { 111 throw new BootstrapException("Failed to bootstrap bean factory", ex); 112 } 113 } 114 115 118 public BeanFactory getBeanFactory() { 119 return bootstrapFactory; 120 } 121 122 } 123 | Popular Tags |