我可以将Maven项目转换为SpringBoot吗?

SSI*_*SSI 7 java eclipse spring maven spring-boot

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

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

Sim*_*ant 11

这里描述了如何将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>
Run Code Online (Sandbox Code Playgroud)


Nee*_*ngh 7

将 Maven 项目转换为 SpringBoot

  • 选项:Spring Boot 使用父 POM

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

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

2.) spring-boot-starter-web :- 用于使用 Spring MVC 构建 Web 的 Starter,包括 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>
Run Code Online (Sandbox Code Playgroud)

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

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

如果您不想使用spring-boot-starter-parent ,您仍然可以通过使用scope=import dependency来保留依赖管理(但不是插件管理)的好处,如下所示:

<dependency> <!-- 从 Spring Boot 导入依赖管理 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.BUILD-SNAPSHOT </version> <type>pom</type>
<scope>import</scope></dependency>

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


小智 5

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

2)在类中添加@SpringBootApplication

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