如果您有 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();
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
内容来源于网络,如有侵权,请联系作者删除!