1 8 9 package org.jboss.spring.kernel; 10 11 import org.springframework.beans.BeansException; 12 import org.springframework.beans.factory.BeanDefinitionStoreException; 13 import org.springframework.beans.factory.BeanFactory; 14 import org.springframework.beans.factory.BeanFactoryAware; 15 import org.springframework.beans.factory.BeanNameAware; 16 import org.springframework.beans.factory.config.*; 17 import org.springframework.core.Ordered; 18 19 22 public class MicrocontainerConfigurer extends MicrocontainerLocatorSupport 23 implements BeanFactoryPostProcessor, BeanNameAware, BeanFactoryAware, Ordered 24 { 25 26 private String beanName; 27 private BeanFactory beanFactory; 28 private int order = Integer.MAX_VALUE; 29 30 private String prefix = "mc${"; 31 32 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess) throws BeansException 33 { 34 BeanDefinitionVisitor visitor = new MicrocontainerBeanDefinitionVisitor(); 35 String [] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); 36 for (int i = 0; i < beanNames.length; i++) 38 { 39 if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) 42 { 43 BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]); 44 try 45 { 46 visitor.visitBeanDefinition(bd); 47 } 48 catch (BeanDefinitionStoreException ex) 49 { 50 throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); 51 } 52 } 53 } 54 } 55 56 protected boolean isMicrocontainerRef(String value) 57 { 58 return value != null && value.startsWith(prefix); 59 } 60 61 protected String parseBeansReference(String value) 62 { 63 int endIndex = value.lastIndexOf("$"); 64 endIndex = endIndex >= 0 ? endIndex : value.length(); 65 return value.substring(prefix.length(), endIndex); 66 } 67 68 private class MicrocontainerBeanDefinitionVisitor extends BeanDefinitionVisitor 69 { 70 71 protected String resolveStringValue(String strVal) 72 { 73 return strVal; 74 } 75 76 protected Object resolveValue(Object value) 77 { 78 value = super.resolveValue(value); 79 if (value instanceof TypedStringValue) 80 { 81 TypedStringValue typedStringValue = (TypedStringValue) value; 82 String beansRef = typedStringValue.getValue(); 83 if (isMicrocontainerRef(beansRef)) 84 { 85 return locateBean(parseBeansReference(beansRef)); 86 } 87 } 88 if (value instanceof String ) 89 { 90 String beansRef = (String ) value; 91 if (isMicrocontainerRef(beansRef)) 92 { 93 return locateBean(parseBeansReference(beansRef)); 94 } 95 } 96 return value; 97 } 98 99 } 100 101 110 public void setBeanName(String beanName) 111 { 112 this.beanName = beanName; 113 } 114 115 124 public void setBeanFactory(BeanFactory beanFactory) 125 { 126 this.beanFactory = beanFactory; 127 } 128 129 public int getOrder() 130 { 131 return order; 132 } 133 134 public void setOrder(int order) 135 { 136 this.order = order; 137 } 138 139 public void setPrefix(String prefix) 140 { 141 this.prefix = prefix; 142 } 143 144 } 145 | Popular Tags |