如何将 Java-config 类导入 XML-config 以便两个上下文都有 bean? [英] How to import Java-config class into XML-config so that both contexts have beans?

查看:13
本文介绍了如何将 Java-config 类导入 XML-config 以便两个上下文都有 bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 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

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

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.

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

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?

推荐答案

这实际上非常简单.要将 Java-config bean 定义放入 xml-config,只需将 Java-config 类定义为 XML-config 中的 bean.不需要额外的罐子.

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.

@Configuration
public class SomeJavaConfig {

    @bean
    ... [bean definition]
}

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

inside the XML-config, you define this class as a 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" />

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

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

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

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

这篇关于如何将 Java-config 类导入 XML-config 以便两个上下文都有 bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆