繁体   English   中英

我可以将 SpringBoot 框架添加到现有的 Maven 项目中吗

[英]Can I Add SpringBoot Framework to Existing Maven Project

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

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

这里描述了如何将 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>

将 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=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>导入</scope> </dependency>

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

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

2)在类中添加@SpringBootApplication

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

是的,你可以使用 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

我将在另外两次采访中面对这个问题,这样它也可以帮助你充实自己。 将标准单独应用程序转换为 Spring Boot

只需右键单击您的项目选择“配置”并选择“转换为 Maven 项目”,然后它将创建或应用程序为 maven 和显式添加我们想要的 Maven 依赖项 .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM