protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (String beanName1 : beanNames) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in properties file locations. if (!(beanName1.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { Properties beanLevelProps = beanLevelProperties.getMergedBeanProperties(beanName1); // add cluster info properties as well beanLevelProps.putAll(ClusterInfoPropertyPlaceholderConfigurer.createProperties(clusterInfo)); BeanDefinitionVisitor visitor = new PlaceholderResolvingBeanDefinitionVisitor(beanLevelProps); BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanName1); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName1, ex.getMessage()); } } } }
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (String beanName1 : beanNames) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in properties file locations. if (!(beanName1.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { BeanDefinitionVisitor visitor = new PlaceholderResolvingBeanDefinitionVisitor(properties); BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanName1); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName1, ex.getMessage()); } } } }
/** * copy & paste, just so we can insert our own visitor. */ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { BeanDefinitionVisitor visitor = new ReloadingPropertyPlaceholderConfigurer.PlaceholderResolvingBeanDefinitionVisitor(props); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (int i = 0; i < beanNames.length; i++) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in net.unicon.iamlabs.spring.properties.example.net.unicon.iamlabs.spring.properties file locations. if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { this.currentBeanName = beanNames[i]; try { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); } } finally { currentBeanName = null; } } } }
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) { BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (String curName : beanNames) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in properties file locations. if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName); try { visitor.visitBeanDefinition(bd); } catch (Exception ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex); } } } // New in Spring 2.5: resolve placeholders in alias target names and aliases as well. beanFactoryToProcess.resolveAliases(valueResolver); // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes. beanFactoryToProcess.addEmbeddedValueResolver(valueResolver); }
public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException { BeanDefinitionVisitor visitor = newVisitor(); String[] beanNames = bf.getBeanDefinitionNames(); for (int i = 0; i < beanNames.length; i++) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in properties file // locations. if (!(beanNames[i].equals(beanName))) { BeanDefinition bd = bf.getBeanDefinition(beanNames[i]); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); } } } }
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if(this.config==null){ this.config = this.beanFactory.getBean(ConfigSetting.class); } StringValueResolver valueResolver = new ConfigSettingStringValueResolver(config); BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); String[] beanNames = beanFactory.getBeanDefinitionNames(); for (String curName : beanNames) { if (!(curName.equals(this.beanName) && beanFactory.equals(this.beanFactory))) { BeanDefinition bd = beanFactory.getBeanDefinition(curName); try { visitor.visitBeanDefinition(bd); } catch (Exception ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage()); } } } }
@Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { StringValueResolver valueResolver = new StringValueResolver() { @Override public String resolveStringValue(String strVal) { StringBuffer sb = new StringBuffer(strVal); /** * 给变量加上${XXXXXXX} */ boolean start = strVal.startsWith(Constant.PLACEHOLDER_PREFIX); boolean end = strVal.endsWith(Constant.PLACEHOLDER_SUFFIX); while(start && end) { String key = sb.substring(placeholderPrefix.length(), sb.length() - placeholderSuffix.length()); String zkValue = ShiZiQiuConfClient.get(key, ""); logger.info(">>>>>>>>>>> resolved placeholder '" + key + "' to value [" + zkValue + "]"); sb = new StringBuffer(zkValue); start = sb.toString().startsWith(placeholderPrefix); end = sb.toString().endsWith(placeholderSuffix); } return sb.toString(); } }; BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); if (null != beanNames && beanNames.length > 0) { for (String beanName : beanNames) { if(!(beanName.equals(this.beanName)) && beanFactoryToProcess.equals(this.beanFactory)) { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanName); visitor.visitBeanDefinition(bd); } } } }
/** * copy & paste, just so we can insert our own visitor. * 启动时 进行配置的解析 */ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { BeanDefinitionVisitor visitor = new ReloadingPropertyPlaceholderConfigurer.PlaceholderResolvingBeanDefinitionVisitor(props); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (int i = 0; i < beanNames.length; i++) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in net.unicon.iamlabs.spring.properties.example.net // .unicon.iamlabs.spring.properties file locations. if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { this.currentBeanName = beanNames[i]; try { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); } } finally { currentBeanName = null; } } } StringValueResolver stringValueResolver = new PlaceholderResolvingStringValueResolver(props); // New in Spring 2.5: resolve placeholders in alias target names and aliases as well. beanFactoryToProcess.resolveAliases(stringValueResolver); // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes. beanFactoryToProcess.addEmbeddedValueResolver(stringValueResolver); }
@Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { // init value resolver StringValueResolver valueResolver = new StringValueResolver() { String placeholderPrefix = "${"; String placeholderSuffix = "}"; @Override public String resolveStringValue(String strVal) { StringBuffer buf = new StringBuffer(strVal); // loop replace by xxl-conf, if the value match '${***}' boolean start = strVal.startsWith(placeholderPrefix); boolean end = strVal.endsWith(placeholderSuffix); while (start && end) { // replace by xxl-conf String key = buf.substring(placeholderPrefix.length(), buf.length() - placeholderSuffix.length()); String zkValue = XxlConfClient.get(key, ""); buf = new StringBuffer(zkValue); logger.info(">>>>>>>>>>> xxl-conf resolved placeholder '" + key + "' to value [" + zkValue + "]"); start = buf.toString().startsWith(placeholderPrefix); end = buf.toString().endsWith(placeholderSuffix); } return buf.toString(); } }; // init bean define visitor BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); // visit bean definition String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); if (beanNames != null && beanNames.length > 0) { for (String beanName : beanNames) { if (!(beanName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanName); visitor.visitBeanDefinition(bd); } } } // TODO Auto-generated method stub //super.processProperties(beanFactoryToProcess, props); }
protected BeanDefinitionVisitor newVisitor() { return new PlaceholderResolvingBeanDefinitionVisitor(); }
/** * Load bean definitions from the specified resource. * * @param element * the import XML DOM element containing the resource * @param visitor * the visitor to resolve the template variables * @param valueResolverIn * the resolver to resolve the bean names * @return the loaded and resolved beans * @throws BeanCreationException * in case of loading non template beans * @throws BeanDefinitionStoreException * in case of loading or parsing errors */ protected Map<String, Tuple<BeanDefinition, Boolean>> loadBeanDefinitions(Element element, BeanDefinitionVisitor visitor, StringValueResolver valueResolverIn) { ImportStringValueResolver valueResolver = (ImportStringValueResolver) valueResolverIn; // load bean definitions to the registry BeanDefinitionRegistry registry = new DefaultListableBeanFactory(); ResourceLoader resourceLoader = new DefaultResourceLoader(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); PropertyResolver propertyResolver = valueResolver.getPropertyResolver(); // load resource String resourceLocation = element.getAttribute("resource"); resourceLocation = propertyResolver.resolvePlaceholders(resourceLocation); Resource resource = resourceLoader.getResource(resourceLocation); // load contents try (Scanner scanner = new Scanner(resource.getInputStream(), "UTF-8")) { String contents = scanner.useDelimiter("\\A").next(); contents = propertyResolver.resolvePlaceholders(contents); // change contents boolean contentsChanged = true; while (contentsChanged) { String prevContents = contents; for (String propIn : valueResolver.mappings.keySet()) { contents = contents.replace("${" + propIn + "}", valueResolver.mappings.get(propIn)); } contentsChanged = !prevContents.equals(contents); } reader.loadBeanDefinitions(new ByteArrayResource(contents.getBytes("UTF-8"))); // resolve bean names Map<String, Tuple<BeanDefinition, Boolean>> beans = new HashMap<String, Tuple<BeanDefinition, Boolean>>(); for (String beanNameIteration : registry.getBeanDefinitionNames()) { BeanDefinition beanDefinition = registry.getBeanDefinition(beanNameIteration); String beanName = beanNameIteration.replace(propPrefixReplacement, "${").replace(propPostfixReplacement, "}"); visitor.visitBeanDefinition(beanDefinition); String resolvedBeanName = valueResolver.resolveStringValue(beanName); boolean isAnonymous = resolvedBeanName.matches("^.*#\\d+$"); /** * Now anonymous beans are imported only once */ beans.put(resolvedBeanName, new Tuple<BeanDefinition, Boolean>(beanDefinition, isAnonymous)); } return beans; } catch (IOException e) { throw new BeanDefinitionStoreException(e.getMessage(), e); } }
/** * Central template method to actually parse the supplied {@link Element} * into one or more {@link BeanDefinition BeanDefinitions}. * * @param element * the element that is to be parsed into one or more * {@link BeanDefinition BeanDefinitions} * @param parserContext * the object encapsulating the current state of the parsing * process; provides access to a * {@link org.springframework.beans.factory.support.BeanDefinitionRegistry} * @return the primary {@link BeanDefinition} resulting from the parsing of * the supplied {@link Element} * @see #parse(org.w3c.dom.Element, ParserContext) * @see #postProcessComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) */ @Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { Map<String, String> variables = getVariables(element); ConfigurableEnvironment environment = (ConfigurableEnvironment) parserContext.getDelegate().getReaderContext() .getEnvironment(); StringValueResolver resolver = new ImportStringValueResolver(variables, environment); BeanDefinitionVisitor visitor = new ImportBeanDefinitionVisitor(resolver); Map<String, Tuple<BeanDefinition, Boolean>> beanDefinitions = loadBeanDefinitions(element, visitor, resolver); registerBeans(element, parserContext, beanDefinitions); return null; }