SKR
SKR

Reputation: 123

Spring XML and java configuration together

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.

Upvotes: 0

Views: 107

Answers (1)

Rafał S
Rafał S

Reputation: 863

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

Upvotes: 2

Related Questions