简体   繁体   English

Spring Boot从库中的上下文xml加载bean

[英]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. 对的,这是可能的。

From javadoc : javadoc

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.

相关问题 如果从web.xml加载spring context.xml,如何获取bean? - How can get beans if spring context.xml load from web.xml? Spring Boot从另一个模块加载bean之前 - Spring Boot load beans from a different module before 我们可以根据我们在 Spring 引导应用程序中的 CONFIG_MODE 为 spring 数据源加载 XML bean 吗? - Can we load XML beans for spring data source according to our CONFIG_MODE in Spring Boot application? 带有xml No Annotation的JUnit Spring无法加载bean - JUnit Spring with xml No Annotation failed to load beans 如何从外部spring上下文中将spring bean注册到spring上下文中? - How to register spring beans into a spring context, from outside spring context? 如何从外部库的spring文件中加载spring上下文 - How to load a spring context from external library's spring files Spring boot 应用上下文中一些bean的依赖形成了一个循环: - Spring boot The dependencies of some of the beans in the application context form a cycle: 应用上下文中一些bean的依赖形成了一个循环:在spring boot - The dependencies of some of the beans in the application context form a cycle: in spring boot Spring Boot 1.5.1和“应用程序上下文中某些Bean的依赖性形成一个循环” - Spring Boot 1.5.1 and “The dependencies of some of the beans in the application context form a cycle” 应用上下文中一些bean的依赖在spring boot中形成循环错误 - The dependencies of some of the beans in the application context form a cycle errors in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM