Mr.Joe
Mr.Joe

Reputation: 25

Spring Boot load beans from context xml in library

I have two applications:

Is possible to load all beans defined in child XML, and put them into parent context?

Upvotes: 1

Views: 4702

Answers (1)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26936

Yes it is possible.

From javadoc:

As mentioned above, @Configuration classes may be declared as regular Spring definitions within Spring XML files. It is also possible to import Spring XML configuration files into @Configuration classes using the @ImportResource annotation. Bean definitions imported from XML can be injected using @Autowired or @Import.

Here an example from the same javadoc that mix beans loaded from xml in beans defined in configuration class:

 @Configuration
 @ImportResource("classpath:/com/acme/database-config.xml")
 public class AppConfig {
     @Inject DataSource dataSource; // from XML

     @Bean
     public MyBean myBean() {
         // inject the XML-defined dataSource bean
         return new MyBean(this.dataSource);
     }
 }

Upvotes: 2

Related Questions