Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 275
Tony Card Avatar asked Nov 06 '12 16:11

Tony Card


People also ask

Can we use both annotation and xml based configuration?

you can use both annotation based configuration and xml based (ControllerClassNameHandlerMapping ) by specifying the order of these handlers. for annotation based configuration we have to provide location ie: where to locate annotated controllers.

What is @configuration and @bean?

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

What are beans in xml?

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container. For example, in the form of XML <bean/> definitions which you have already seen in the previous chapters.

Which annotation are used for Java based beans configuration?

Some important annotations used for java based configuration are @Configuration, @ComponentScan and @Bean.


1 Answers

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.

like image 198
Tony Card Avatar answered Sep 21 '22 17:09

Tony Card