我可以将Maven项目转换为Spring Boot

问题描述 投票:6回答:3

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

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

java eclipse maven spring-boot
3个回答
9
投票

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项目转换为Spring Boot

  • 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 = import依赖项来保持依赖项管理(但不是插件管理)的好处,如下所示:

<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 Docs: - https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#getting-started-maven-installation


1
投票

1)在Pom.xml文件中添加Spring启动程序Parent和Web

2)在主类中添加@SpringBootApplication

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

© www.soinside.com 2019 - 2024. All rights reserved.