【问题标题】:Can I convert Maven Project to SpringBoot我可以将 Maven 项目转换为 Spring Boot
【发布时间】:2015-06-04 07:40:26
【问题描述】:

我在 Eclipse 中有一个 Maven 项目,现在我需要添加数据库连接。我的教科书在 Maven 中完成了所有 json 教程。现在在关于 JDBC 的这一章中,他们正在使用 SpringBoot。

我可以将项目转换为 SpringBoot 吗?或者启动一个 SpringBoot 并导入我以前的 Maven 类。

【问题讨论】:

  • 看看这个start.spring.io,你可以从 spring boot pom.xml 和父级以及必要的东西中添加适当的依赖项。
  • 这是一篇旧帖,仅供参考。 Spring Boot 无疑是一种有效的(并且对许多人来说更可取)添加数据库连接的方法。然而,这绝不是唯一的方法。您不必在项目中使用 Spring Boot 来与 JDBC 对话。可以通过包含必要的依赖项来完成。

标签: java spring eclipse maven spring-boot


【解决方案1】:

Here is described如何使用maven进行SpringBoot项目。

您需要修改现有的pom.xml 以添加类似这样的内容以使其成为 SpringBoot 项目:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.2.RELEASE</version>
</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【讨论】:

    【解决方案2】:

    将 Maven 项目转换为 SpringBoot

    • Ist 选项:Spring Boot 使用父 POM

    在 pom.xml 文件中添加以下依赖项**:-

    1.) 继承启动器父级(spring-boot-starter-parent):spring-boot-starter-parent 是一个特殊的启动器,它提供有用的 Maven 默认值。

    2.) spring-boot-starter-web :- 使用 Spring MVC 构建 web,包括 RESTful 应用程序的启动器。使用 Tomcat 作为默认的嵌入式容器。

    3.) spring-boot-maven-plugin:- Spring Boot 包含一个 Maven 插件,可以将项目打包为可执行 jar。

    这里是 pom.xml :-

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
    </parent>
    
    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    • 第二个选项:在没有父 POM 的情况下使用 Spring Boot

    不是每个人都喜欢从 spring-boot-starter-parent POM 继承。您可能需要使用自己的公司标准父级,或者您可能更愿意显式声明所有 Maven 配置。

    如果你不想使用 spring-boot-starter-parent,你仍然可以通过使用 scope 来保持依赖管理(但不是插件管理)的好处=导入依赖,如下:


    org.springframework.boot
    spring-boot-dependencies
    2.0.0.BUILD-SNAPSHOTpom
    导入

    在 Spring Boot Docs 中找到更多详细信息:- https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#getting-started-maven-installation

    【讨论】:

    【解决方案3】:

    1) 在Pom.xml文件中添加Spring boot starter Parent和Web

    2) 在ma​​in类中添加@SpringBootApplication

    3) 添加 SpringApplication.run(App.class, args);在 main 方法中。

    【讨论】:

    • 没有主类怎么办?如果它是像库模块这样的子模块呢?
    • 你不必有主课。 spring boot 库也是可以的。你不必使用 SpringBootApplciaiton
    【解决方案4】:

    是的,你可以使用 Springboot 和 Maven 作为依赖管理。 您将不得不添加 Spring Boot 依赖项

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    

    还在主类中添加@SpringBootApplication 和@EnableAutoConfiguration

    【讨论】:

      【解决方案5】:

      我将在另外两次采访中面对这个问题,这样它也可以帮助你充实自己。

      只需右键单击您的项目选择“配置”并选择“转换为 Maven 项目”,然后它将创建或应用程序为 maven 和 。

      【讨论】:

        猜你喜欢
        • 2019-02-18
        • 2017-10-19
        • 2017-02-15
        • 2019-12-16
        • 1970-01-01
        • 2020-08-25
        • 2017-07-30
        • 2012-07-24
        • 1970-01-01
        相关资源
        最近更新 更多