将现有的 spring 应用程序迁移到 spring-boot,手动配置 spring-boot?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31409231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:41:05  来源:igfitidea点击:

Migrate existing spring app to spring-boot, manually configure spring-boot?

springspring-boot

提问by bitsofinfo

I have an existing spring 3.1.4 application that works fine and boots up ok on its own. I currently start the spring context manually in a main class of my own. This is NOT a spring-mvc app, it does not contain any servlets, web.xml nor does it generate a WAR. It just produces a JAR for an integration backend.

我有一个现有的 spring 3.1.4 应用程序,它可以正常工作并且可以自行启动。我目前在我自己的主类中手动启动 spring 上下文。这不是一个 spring-mvc 应用程序,它不包含任何 servlet、web.xml 也不生成 WAR。它只是为集成后端生成一个 JAR。

I would like "wrap" this legacy application and launch it with spring-boot. However I am having trouble figuring out how to do this as all the examples seem to assume creating a "new" application.

我想“包装”这个遗留应用程序并使用 spring-boot 启动它。但是,我无法弄清楚如何执行此操作,因为所有示例似乎都假设创建一个“新”应用程序。

1) I have my existing applicationContext.xml file with my existing spring app bean declarations in it

1) 我有我现有的 applicationContext.xml 文件,其中包含我现有的 spring 应用程序 bean 声明

2) What is the minimum set of new bean configs that I need to add to my existing Spring applicationContext.xml file in order to have spring-boot w/ tomcat launched and load all of my existing beans into the spring-boot wrapped context?

2) 为了启动带有 tomcat 的 spring-boot 并将所有现有 bean 加载到 spring-boot 包装上下文中,我需要添加到现有 Spring applicationContext.xml 文件中的最少新 bean 配置集是多少?

Can anyone point me in the right direction please?

任何人都可以指出我正确的方向吗?

回答by hzpz

There is a chapter dedicated to Converting an existing application to Spring Bootin the Spring Boot reference guide.

在 Spring Boot 参考指南中有一章专门介绍将现有应用程序转换为 Spring Boot

Basically you need to add the Spring Boot dependencies and then implement the main entry point like this:

基本上你需要添加 Spring Boot 依赖项,然后像这样实现主入口点:

@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

However, this will also trigger Spring Boot's auto-configuration based upon (among other things) available classes and configured beans. You might want to disable certain auto-configurations. To exclude DataSource and Hibernate JPA auto-configuration, use:

但是,这也将触发 Spring Boot 的基于(除其他外)可用类和配置的 bean 的自动配置。您可能想要禁用某些自动配置。要排除 DataSource 和 Hibernate JPA 自动配置,请使用:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })