1 16 17 package org.springframework.beans.factory.support; 18 19 import org.springframework.beans.MutablePropertyValues; 20 import org.springframework.beans.factory.BeanDefinitionStoreException; 21 import org.springframework.beans.factory.BeanFactoryUtils; 22 import org.springframework.beans.factory.config.BeanDefinitionHolder; 23 import org.springframework.beans.factory.config.ConstructorArgumentValues; 24 import org.springframework.util.ClassUtils; 25 import org.springframework.util.ObjectUtils; 26 import org.springframework.util.StringUtils; 27 28 38 public class BeanDefinitionReaderUtils { 39 40 44 public static final String GENERATED_BEAN_NAME_SEPARATOR = BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR; 45 46 47 61 public static AbstractBeanDefinition createBeanDefinition( 62 String className, String parent, ConstructorArgumentValues cargs, 63 MutablePropertyValues pvs, ClassLoader classLoader) throws ClassNotFoundException { 64 65 AbstractBeanDefinition bd = createBeanDefinition(parent, className, classLoader); 66 bd.setConstructorArgumentValues(cargs); 67 bd.setPropertyValues(pvs); 68 return bd; 69 } 70 71 81 public static AbstractBeanDefinition createBeanDefinition( 82 String parent, String className, ClassLoader classLoader) throws ClassNotFoundException { 83 84 AbstractBeanDefinition bd = null; 85 if (parent != null) { 86 bd = new ChildBeanDefinition(parent); 87 } 88 else { 89 bd = new RootBeanDefinition(); 90 } 91 if (className != null) { 92 if (classLoader != null) { 93 bd.setBeanClass(ClassUtils.forName(className, classLoader)); 94 } 95 else { 96 bd.setBeanClassName(className); 97 } 98 } 99 return bd; 100 } 101 102 115 public static String generateBeanName( 116 AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry beanFactory, boolean isInnerBean) 117 throws BeanDefinitionStoreException { 118 119 String generatedId = beanDefinition.getBeanClassName(); 120 if (generatedId == null) { 121 if (beanDefinition instanceof ChildBeanDefinition) { 122 generatedId = ((ChildBeanDefinition) beanDefinition).getParentName() + "$child"; 123 } 124 else if (beanDefinition.getFactoryBeanName() != null) { 125 generatedId = beanDefinition.getFactoryBeanName() + "$created"; 126 } 127 } 128 if (!StringUtils.hasText(generatedId)) { 129 throw new BeanDefinitionStoreException("Unnamed bean definition specifies neither " + 130 "'class' nor 'parent' nor 'factory-bean' - can't generate bean name"); 131 } 132 133 String id = generatedId; 134 if (isInnerBean) { 135 id = generatedId + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(beanDefinition); 137 } 138 else { 139 int counter = 0; 142 while (beanFactory.containsBeanDefinition(id)) { 143 counter++; 144 id = generatedId + GENERATED_BEAN_NAME_SEPARATOR + counter; 145 } 146 } 147 return id; 148 } 149 150 160 public static String generateBeanName( 161 AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry beanFactory) 162 throws BeanDefinitionStoreException { 163 164 return generateBeanName(beanDefinition, beanFactory, false); 165 } 166 167 173 public static void registerBeanDefinition( 174 BeanDefinitionHolder bdHolder, BeanDefinitionRegistry beanFactory) 175 throws BeanDefinitionStoreException { 176 177 String beanName = bdHolder.getBeanName(); 179 beanFactory.registerBeanDefinition(beanName, bdHolder.getBeanDefinition()); 180 181 String [] aliases = bdHolder.getAliases(); 183 if (aliases != null) { 184 for (int i = 0; i < aliases.length; i++) { 185 beanFactory.registerAlias(beanName, aliases[i]); 186 } 187 } 188 } 189 190 199 public static String registerWithGeneratedName( 200 AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry beanFactory) 201 throws BeanDefinitionStoreException { 202 203 String generatedName = generateBeanName(beanDefinition, beanFactory, false); 204 beanFactory.registerBeanDefinition(generatedName, beanDefinition); 205 return generatedName; 206 } 207 208 } 209 | Popular Tags |