【问题标题】:Placeholder not working on custom schema element占位符不适用于自定义架构元素
【发布时间】:2014-07-17 08:45:18
【问题描述】:

我正在尝试从占位符中获取一些值到自定义架构属性(cfg:merge-elements' 目标),但它不起作用:

somexml.xml:

<bean id="stateMachineNamingStrategy" class="com.mycompany.statemachine.hibernate.StateMachineNamingStrategy">
    <property name="prefix" value="${statemachine.table_prefix}"/>
</bean>

<cfg:merge-elements id="packages-com.mycompany.statemachine" target="${session-factory-pkgs}">
    <value>com.mycompany.statemachine.machine.impl</value>
</cfg:merge-elements>

这些定义在同一个 xml 中。第一个(stateMachineNamingStrategy)正确地从 ${statemachine.table_prefix} 获取值。

第二个无法在 ${session-factory-pkgs} 中检索它。异常显示属性值没有被转换:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '${session-factory-pkgs}' is defined

为什么不将占位符转换为它的值??

【问题讨论】:

    标签: spring spring-mvc dependency-injection


    【解决方案1】:

    如果有人遇到同样的问题:

    事实证明,在我的 NamespaceHandler 中,我注册了一个扩展 BeanDefinitionRegistryPostProcessor 的新 BeanDefinition。这些特殊的后处理器在正常BeanFactoryPostProcessor 之前执行,以允许注册或删除 BeanDefinition。因此,没有办法让 PropertyPlaceholderConfigurer(这是一个普通的 BeanPostProcessor)在 BeanDefinitionRegistryPostProcessor 之前执行。

    你可以在 XmlWebApplicationContext(AbstractApplicationContext).invokeBeanFactoryPostProcessors 中看到这一切:

            Map<String, BeanDefinitionRegistryPostProcessor> beanMap =
                    beanFactory.getBeansOfType(BeanDefinitionRegistryPostProcessor.class, true, false);
            List<BeanDefinitionRegistryPostProcessor> registryPostProcessorBeans =
                    new ArrayList<BeanDefinitionRegistryPostProcessor>(beanMap.values());
            OrderComparator.sort(registryPostProcessorBeans);
            for (BeanDefinitionRegistryPostProcessor postProcessor : registryPostProcessorBeans) {
    
                //!!!!! BeanDefinitionRegistryPostProcessors are excecuted!!!!!
                postProcessor.postProcessBeanDefinitionRegistry(registry);
            }
            invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
            invokeBeanFactoryPostProcessors(registryPostProcessorBeans, beanFactory);
            invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
            processedBeans.addAll(beanMap.keySet());
        }
        else {
            // Invoke factory processors registered with the context instance.
            invokeBeanFactoryPostProcessors(getBeanFactoryPostProcessors(), beanFactory);
        }
    
        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let the bean factory post-processors apply to them!
        String[] postProcessorNames =
                beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    
        // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.
        List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
        List<String> orderedPostProcessorNames = new ArrayList<String>();
        List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
        for (String ppName : postProcessorNames) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-15
      • 2015-03-28
      • 2014-09-05
      • 2012-01-27
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多