56

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?

Tony Card
  • 2,084
  • 3
  • 20
  • 27

3 Answers3

104

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.

Tony Card
  • 2,084
  • 3
  • 20
  • 27
  • 34
    Sidenote: must exist in your XML config as well, otherwise your javaconfig won't be included. – Alan Franzoni Aug 30 '13 at 11:19
  • if you already define ConfigurationPostProcessor in your XML config, not needed! –  Jan 26 '17 at 19:54
  • tag just added all the springs PostProcessors for you. –  Jan 26 '17 at 19:55
  • can we use as we do for another xml config file. –  Jan 26 '17 at 19:58
  • Is the forward slash intentional? – coderatchet May 22 '17 at 23:02
  • @thenaglecode- no... that was probably a typo (when I was copy/pasting from a real-world example and editing for this post). The value of the bean name can be whatever you want it to be -I generally leave them the name of the class (unless there's some other reason to change it) – Tony Card May 23 '17 at 16:22
  • 1
    Please add the hint of Alan Franzoni to you answer - this was helpful. – Dirk Oct 20 '17 at 14:21
  • There is no need for `` - and it might pickup all available Configurations. I'm working with profiles, thus I define one Configuration bean per profile and it is working. If you really need scanning, use the ComponentScan annotation at your Config class. – bebbo Nov 15 '19 at 08:00
7

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 Difference between <context:annotation-config> vs <context:component-scan> for more details.

Matthias M
  • 12,906
  • 17
  • 87
  • 116
-1

Should be in:

spring-javaconfig-<version>.jar
maximdim
  • 8,041
  • 3
  • 33
  • 48
  • 10
    Evidently, the spring-javaconfig jar was decomissioned. It was supposed to be migrated into Spring Core in version 3.0. – Tony Card Nov 06 '12 at 19:24