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

SpringConfiguration

Spring Problem Overview


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

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

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?

Spring Solutions


Solution 1 - Spring

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

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

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

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

Solution 2 - Spring

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

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

See https://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan for more details.

Solution 3 - Spring

Should be in:

spring-javaconfig-<version>.jar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTony CardView Question on Stackoverflow
Solution 1 - SpringTony CardView Answer on Stackoverflow
Solution 2 - SpringMatthias MView Answer on Stackoverflow
Solution 3 - SpringmaximdimView Answer on Stackoverflow