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

查看:196
本文介绍了如何将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。话虽如此,上下文显然是完全分离的。我想'import'一个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配置bean定义放到xml-config中,只需将XML配置中的Java-config类定义为bean即可。没有额外的jar需要。

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



< config,你将这个类定义为一个bean。

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

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

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

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

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

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