[英]Spring Boot load beans from context xml in library
I have two applications: 我有两个应用程序:
Application child
: it's a Spring app with XML Schema-based configuration. Application child
:这是具有基于XML Schema的配置的Spring应用程序。 So We have an applicationContext.xml
. 因此,我们有一个applicationContext.xml
。
Application parent
: Spring Boot app. parent
应用程序:Spring Boot应用程序。 Uses application child
as a library. 将应用程序child
用作库。
Is possible to load all beans defined in child
XML, and put them into parent
context? 是否可以加载在child
XML中定义的所有bean,并将它们放入parent
上下文中?
Yes it is possible. 对的,这是可能的。
As mentioned above, @Configuration classes may be declared as regular Spring definitions within Spring XML files. 如上所述,@ Configuration类可以在Spring XML文件中声明为常规Spring定义。 It is also possible to import Spring XML configuration files into @Configuration classes using the @ImportResource annotation . 也可以使用@ImportResource批注将Spring XML配置文件导入@Configuration类 。 Bean definitions imported from XML can be injected using @Autowired or @Import. 从XML导入的Bean定义可以使用@Autowired或@Import注入。
Here an example from the same javadoc that mix beans loaded from xml in beans defined in configuration class: 这是来自同一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.