【问题标题】:Spring Boot load beans from context xml in librarySpring Boot从库中的上下文xml加载bean
【发布时间】:2017-08-21 16:50:09
【问题描述】:

我有两个应用程序:

  • 应用程序child:它是一个具有基于 XML Schema 配置的 Spring 应用程序。所以我们有一个applicationContext.xml

  • 应用程序parent:Spring Boot 应用程序。使用应用程序child 作为库。

是否可以加载child XML 中定义的所有bean,并将它们放入parent 上下文中?

【问题讨论】:

    标签: java spring spring-boot dependencies


    【解决方案1】:

    是的,这是可能的。

    来自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);
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-09
      • 2021-09-02
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多