eclipse 使用maven 构建springboot 程序解析

符号 阅读:621 2021-03-31 21:22:04 评论:0

1、创建Maven工程

打开Eclipse,点击File->New->Other,在弹出对话框中,选中Maven Project。 
这里写图片描述

点击Next按钮,出现下图,根据自己需要设置,可以使用默认的。 
这里写图片描述

再点击Next按钮,出现下图,选中图中背景为蓝色的项。 
这里写图片描述

再点击Next按钮,设置Group Id和Artifact Id,其他项可以不用设置。 
这里写图片描述

点击Finish按钮,完成项目的创建。

2、编写pom.xml

在parent部分使用spring-boost-starter-parent。spring-boost-starter-parent是重要的默认的父工程,它提供了dependency-management部分。

<parent> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-parent</artifactId> 
  <version>1.4.3.RELEASE</version> 
</parent> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

如果我们向pom.xml添加spring-boot-starter-web依赖(在parent这部分之后):

<dependencies> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
</dependencies> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

此时pom.xml如下所示: 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
	<modelVersion>4.0.0</modelVersion> 
	<groupId>com.wlsq.accounts</groupId> 
	<artifactId>WlsqAccounts</artifactId> 
	<version>0.0.1-SNAPSHOT</version> 
 
	<parent> 
		<groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-starter-parent</artifactId> 
		<version>1.4.3.RELEASE</version> 
	</parent> 
 
	<dependencies> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-web</artifactId> 
		</dependency> 
		<dependency> 
			<groupId>junit</groupId> 
			<artifactId>junit</artifactId> 
			<version>3.8.1</version> 
			<scope>test</scope> 
		</dependency> 
	</dependencies> 
</project>

3、测试Spring Boot 应用

在src/main/java目录下,新建一个com.zzg.apple包,然后在包下面新建一个类。

package com.wlsq.app; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@RestController 
@EnableAutoConfiguration 
public class AppleApplication { 
  @RequestMapping("/") 
     String home() { 
         return "欢迎使用SpringBoot!"; 
     } 
     public static void main(String[] args) { 
         SpringApplication.run(AppleApplication.class, args); 
     } 
} 

启动程序,右键AppleApplication.java文件,选择run as -> Java Application。  
如果没有报错的话,在浏览器中输入:  http://localhost:8080/  


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

全民解析

全民解析

关注我们