[英]How can get beans if spring context.xml load from web.xml?
[英]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.