0

I have a project with XML based spring configuration and want to define some new beans but in java class based configuration.

How to implement this so that i can also refer beans of java configuration in my XML config file.

SKR
  • 103
  • 1
  • 10

1 Answers1

2

You can import your xml config into java configuration, like so:

@Configuration
@ImportResource("classpath:pl/rav/springtest/resources/app.xml")
public class AppConfig {
    @Bean(name="myMessageService")
    MessageService mockMessageService() {
        return new MessageServiceImpl();
    }
}

When you want to refer from xml to the bean you just point to its name:

<property name="msgSrv">
    <ref bean="myMessageService"/>
</property>

Then use ApplicationContext based on your java configuration.

    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

As you started with xml config, you may be interested in other way around (importing java config into xml config) which I think was explained here

Rafał S
  • 813
  • 7
  • 13