Spring Boot load beans from context xml in library

吃可爱长大的小学妹 提交于 2019-12-12 06:08:06

问题


I have two applications:

  • Application child: it's a Spring app with XML Schema-based configuration. So We have an applicationContext.xml.

  • Application parent: Spring Boot app. Uses application child as a library.

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


回答1:


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);
     }
 }


来源:https://stackoverflow.com/questions/43072510/spring-boot-load-beans-from-context-xml-in-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!