spring 如何将 Java-config 类导入 XML-config 以便两个上下文都有 bean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13254779/
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-09-08 05:29:59  来源:igfitidea点击:

How to import Java-config class into XML-config so that both contexts have beans?

springconfiguration

提问by Tony Card

I have a project where I need to bootstrap @Configuration java-config classes into the XML configuration.

我有一个项目,我需要将 @Configuration java-config 类引导到 XML 配置中。

To do that, I'm reading that I also need to include the following bean definition (along with the bean definitions of the classes annotated with @Configuration).

为此,我读到我还需要包含以下 bean 定义(以及用 @Configuration 注释的类的 bean 定义)。

<bean class="org.springframework.config.java.process.ConfigurationPostProcessor" />

But, I end up receiving the following error:

但是,我最终收到以下错误:

Caused by: java.lang.ClassNotFoundException: org.springframework.config.java.process.ConfigurationPostProcessor

I have to assume I'm missing a jar somewhere, but my various web searches hasn't resulted in an answer yet. Any help would be greatly appreciated. Thanks.

我不得不假设我在某处丢失了一个罐子,但是我的各种网络搜索还没有得到答案。任何帮助将不胜感激。谢谢。

EDIT: Evidently, I was reading old documentation, which is no longer current. Let me back up. My project contains older XML-based configuration. The newer code is all using 'Java-config'. With that said, the contexts are apparently completely separate. I'd like to 'import' a java-config class into the XML configuration, so that both contexts have those particular beans. Does anyone know how I can do that?

编辑:显然,我正在阅读旧文档,它不再是最新的。让我备份。我的项目包含较旧的基于 XML 的配置。较新的代码全部使用“Java-config”。话虽如此,上下文显然是完全分开的。我想将 java-config 类“导入”到 XML 配置中,以便两个上下文都具有那些特定的 bean。有谁知道我该怎么做?

回答by Tony Card

This actually ended up being fairly simple. To get a Java-config bean definition into the xml-config, simply define the Java-config class as a bean within the XML-config. There are no extra jars necessary.

这实际上最终相当简单。要将 Java-config bean 定义放入 xml-config,只需将 Java-config 类定义为 XML-config 中的 bean。不需要额外的罐子。

@Configuration
public class SomeJavaConfig {

    @bean
    ... [bean definition]
}

inside the XML-config, you define this class as a bean.

在 XML-config 中,您将此类定义为 bean。

<!-- needed to pick up the annotated java-config -->
<context:annotation-config />

<!-- Importing java-config class, which are annotated with @Configuration -->
<bean name="SomeJavaConfig" class="[fully qualified path].SomeJavaConfig" />

The XML-config, which may be part of a different context, now has all the bean definitions defined within the JavaConfig class.

XML-config 可能是不同上下文的一部分,现在具有 JavaConfig 类中定义的所有 bean 定义。

UPDATED- to included Alan Franzoni's comment below in the answer.

更新- 在答案中包含 Alan Franzoni 的评论。

回答by Matthias M

Alternatively to annotation-configyou can use component-scan. Then you do not have to include the Configuration Bean in XML:

或者,annotation-config您可以使用component-scan. 那么您不必在 XML 中包含配置 Bean:

<context:component-scan base-package="[fully qualified package path]" />

See Difference between <context:annotation-config> vs <context:component-scan>for more details.

有关更多详细信息,请参阅<context:annotation-config> 与 <context:component-scan> 之间的区别

回答by maximdim

Should be in:

应该在:

spring-javaconfig-<version>.jar