如何在 Spring Boot 应用程序中使用 Spring

x33g5p2x  于2022-10-05 转载在 Spring  
字(1.0k)|赞(0)|评价(0)|浏览(355)

如果您有 Spring 应用程序,使用多个 XML 配置文件,您仍然可以将它们与 Spring Boot 集成。 让我们看看本教程中的方法。

导入 Spring 应用程序基本上需要导入它们的资源。 在下面的示例中,我们使用 @ImportResource 导入上下文文件。 :

@ImportResource({
  "META-INF/spring/services-context.xml",
  "META-INF/spring/repositories- context.xml"
})
@SpringBootApplication
public class SpringApplication {
  @Autowired TaskRepository task;
  @Autowired ServiceFacade service;
  //More logic...  
}

为了访问 bean,我们将它们自动连接到类成员。 这是另一个示例,它显示了如何重用类路径中的现有 XML 配置文件。

@ImportResource("classpath:applicationContext.xml") @Configuration public class XMLConfiguration {
  @Autowired Connection connection;
  //Derived from the applicationContext.xml file. 
  @Bean Database getDatabaseConnection() {
    return connection.getDBConnection();
  }
}

以下代码显示了如何使用 ConfigurableApplicationContext 在现有 Java 配置类中重用 XML。 您还可以使用主类方法来获取现有的 XML 文件:

public class Application {
  public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx =
        new SpringApplication("/META-INF/spring/integration.xml").run(args);
    System.out.println("Hit Enter to terminate");
    System.in.read();
    ctx.close();
  }
}

相关文章

微信公众号

最新文章

更多