Spring Boot从库中的上下文xml加载bean [英] Spring Boot load beans from context xml in library
问题描述
我有两个应用程序:
-
应用程序
child
:一个具有基于XML Schema的配置的Spring应用程序。因此,我们有一个applicationContext.xml
。
Application
child
: it's a Spring app with XML Schema-based configuration. So We have anapplicationContext.xml
.
应用程序父级
:Spring Boot应用程序。将应用程序 child
用作库。
Application parent
: Spring Boot app. Uses application child
as a library.
可以加载 child
XML中定义的所有bean ,并将其放入 parent
上下文中?
Is possible to load all beans defined in child
XML, and put them into parent
context?
推荐答案
是的
来自 javadoc :
如上所述,@Configuration类可以在Spring XML文件中声明为常规的Spring定义。 也可以使用@ImportResource批注将Spring XML配置文件导入@Configuration类。可以使用@Autowired或@Import注入从XML导入的Bean定义。
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.
以下是同一javadoc中的一个示例,该示例混合了从配置类中定义的bean中的xml:
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);
}
}
这篇关于Spring Boot从库中的上下文xml加载bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!