【问题标题】:systemProperties and integrationGlobalProperties conflictsystemProperties 和集成GlobalProperties 冲突
【发布时间】:2014-04-08 07:34:16
【问题描述】:

所以我有一种情况需要使用 spring 集成。所以我为它创建了应用程序上下文,然后我在其中描述了我的所有逻辑。但是现在,我有一个类似这样的错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [java.util.Properties] is defined: expected single matching bean but found 2: [integrationGlobalProperties, systemProperties]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1184)
    ... 61 more

是否有人遇到过 systemProperties 和 integrationGlobalProperties 的问题?这是什么意思?

附:我的应用程序上下文被导入另一个具有“default-autowire="byType"

的应用程序上下文

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    如您所见,您的上下文有两个 java.util.Properties bean。这意味着你不能注入byType

    使用@Qualifier("systemProperties") 将其限制为特定的Properties

    integrationGlobalProperties 不仅仅是一个由 Framework 填充的 bean。 autowire byType 对实际应用越来越不利也就不足为奇了。

    【讨论】:

    • 放在哪里?我找不到在 systemProperties 上放置注释的位置:/ 因为这些属性来自 spring..
    • 请说明你是如何自动接线的。
    • 我没有,因为它来自 spring 本身。它不是一个自定义的系统属性类。我只是为一些我需要的类设置了 default-autowire="byName",但没有在任何地方指定 systemProperties。现在我与集成属性和系统属性发生冲突,如果我理解得很好,它们是常量。
    • 你有一些与Properties 自动连接的bean。您必须为此指定byName。您可以通过 StackTrace 查看哪个 bean 受到影响。
    • 是的,第一眼看起来很简单,但是没有名为“systemProperties”的bean :)
    【解决方案2】:

    我最近遇到了同样的问题。经过一番研究,我决定编写一个 CustomBeanPostProcessor 类(实现 BeanFactoryPostProcessor),它将尝试找到冲突的 bean 并将其 'autowire' 属性更改为 'byName',从而避免冲突。

    请看下面的代码:

    public class CustomBeanPostProcessor implements BeanFactoryPostProcessor{
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            if (beanFactory.containsBean("integrationGlobalProperties")){
               //Choose on of the options - either disable autowiring for bean completly,
               //or change autowiring type
               beanFactory.getBeanDefinition("integrationGlobalProperties").setAutowireCandidate(false);
               beanFactory.getBeanDefinition("integrationGlobalProperties").setAttribute("autowire", "byName");
            } 
        }
    }
    

    不要忘记将这个类也声明为 Spring bean:

    <bean class="edu.stackoverflow.spring.misc.CustomBeanPostProcessor"/>
    

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 2021-06-17
      • 2020-03-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2019-02-10
      相关资源
      最近更新 更多