簡體   English   中英

Spring Boot從庫中的上下文xml加載bean

[英]Spring Boot load beans from context xml in library

我有兩個應用程序:

  • Application child :這是具有基於XML Schema的配置的Spring應用程序。 因此,我們有一個applicationContext.xml

  • parent應用程序:Spring Boot應用程序。 將應用程序child用作庫。

是否可以加載在child XML中定義的所有bean,並將它們放入parent上下文中?

對的,這是可能的。

javadoc

如上所述,@ Configuration類可以在Spring XML文件中聲明為常規Spring定義。 也可以使用@ImportResource批注將Spring XML配置文件導入@Configuration類 從XML導入的Bean定義可以使用@Autowired或@Import注入。

這是來自同一javadoc的示例,該示例將從xml加載的bean混合到配置類中定義的bean中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM