Java 覆盖 Spring bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19777997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:08:02  来源:igfitidea点击:

Overriding Spring bean

javaspring

提问by OKAN

I have the following scenario:

我有以下场景:

  1. Spring project A with multiple bean configurations, including a bean named "searchHelper":
    <bean name="searchHelper" class="com.test.SearchHelperImpl"/>
    where SearchHelperImplimplements "SearchHelper" interface
  2. Spring project B depends on A with a custom SearchHelperBImpl
  1. Spring项目A有多个bean配置,包括一个名为“searchHelper”的bean:
    <bean name="searchHelper" class="com.test.SearchHelperImpl"/>
    其中SearchHelperImpl实现了“SearchHelper”接口
  2. Spring 项目 B 依赖于带有自定义SearchHelperBImpl 的A

What I was thinking of making is just copying the whole configuration into the new project and changing what needs to be changed, but that's not convenient and there must be an easier way of doing this.

我想做的只是将整个配置复制到新项目中并更改需要更改的内容,但这并不方便,必须有更简单的方法来做到这一点。

My question is, how do I override the definition of the "searchHelper" bean to use SearchHelperBImplinstead of SearchHelperImpl? I want to use the same bean name in order for everything that uses this name to use the new implementation. I am using Spring 3.2.2

我的问题是,如何覆盖“searchHelper”bean 的定义以使用SearchHelperBImpl而不是SearchHelperImpl?我想使用相同的 bean 名称,以便使用此名称的所有内容都使用新实现。我正在使用 Spring 3.2.2

Thanks

谢谢

采纳答案by informatik01

NOTE
This answer relates to how to avoid duplicatebean definitions. For overriding see the answer by nicholas.hauschild.

注意
此答案与如何避免重复的bean 定义有关。对于覆盖,请参阅nicholas.hauschild答案



More effective solution to avoid copying is to place all the beans that are common for both projects in a separate XML configuration file, say "common-beans.xml". And in the configuration XML file for the project B (and any other project that needs those beans) you importthat file like this:

避免复制的更有效解决方案是将两个项目通用的所有 bean 放在单独的 XML 配置文件中,例如“common-beans.xml”。在项目 B(以及任何其他需要这些 bean 的项目)的配置 XML 文件中,您可以像这样导入该文件:

<import resource="common-beans.xml" />

<import resource="common-beans.xml" />



Simple example

简单的例子

example-context.xml

示例上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"

    <!-- 
         Assuming common-beans.xml is a Spring config file
         that contains the definitions you need to use
         in the current project. 
    -->
    <import resource="common-beans.xml" />

    <!-- Defining a bean with the implementaion you need -->
    <bean name="searchHelper" class="com.test.SearchHelperBImpl"/>

    <!-- Some other configurations if needed -->

</beans>


Useful reading:

有用的阅读:

回答by nicholas.hauschild

You should be able to utilize the primaryxml attribute on your beanelement.

您应该能够primary在您的bean元素上使用xml 属性。

<bean name="searchHelper" primary="true" class="com.test.SearchHelperBImpl"/>

Alternatively, if you are using JavaConfig, you can utilize the @Primaryannotation.

或者,如果您使用的是 JavaConfig,则可以使用@Primary注释。

@Primary
@Bean
public SearchHelper searchHelper() {
    return new SearchHelperBImpl();
}

回答by digitaljoel

One interesting "feature" (some consider it a bug) of Spring is that a bean with the same name declared later in configuration will override a bean declared earlier in configuration. So if your project B depends on A, and the configuration in A is included in B, and B defines a bean by the same name after the A configuration then the B instance will "win" and that's the instance you will get.

Spring 的一个有趣的“特性”(有些人认为它是一个错误)是在配置中稍后声明的同名 bean 将覆盖在配置中早先声明的 bean。因此,如果您的项目 B 依赖于 A,并且 A 中的配置包含在 B 中,并且 B 在 A 配置之后定义了一个同名 bean,那么 B 实例将“获胜”,这就是您将获得的实例。

I would not recommend depending on this behavior, but would go with the answer regarding the Primary annotation. I just thought I would bring this up so you would be aware that even without the primary, or in case the one in project A is alsoprimary you would know that the latest definition wins.

我不建议依赖于这种行为,但会使用有关主要注释的答案。我只是想我会提出这个问题,所以你会知道,即使没有主要,或者如果项目 A 中的也是主要的,你就会知道最新的定义会获胜。