当前位置: 移动技术网 > IT编程>开发语言>Java > 使用IDEA创建SpringBoot项目的方法步骤

使用IDEA创建SpringBoot项目的方法步骤

2019年07月19日  | 移动技术网IT编程  | 我要评论

1.打开idea,创建新项目,选择spring initializr


2.输入artifact


3.勾选web


4.点击finish完成

5.进入项目,可以将以下内容删除


pom.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<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.example</groupid> 
 <artifactid>springbootdemo</artifactid> 
 <version>0.0.1-snapshot</version> 
 <packaging>jar</packaging> 
 
 <name>springbootdemo</name> 
 <description>demo project for spring boot</description> 
 
 <!--起步依赖--> 
 <parent> 
  <groupid>org.springframework.boot</groupid> 
  <artifactid>spring-boot-starter-parent</artifactid> 
  <version>1.5.2.release</version> 
  <relativepath/> <!-- lookup parent from repository --> 
 </parent> 
 
 <properties> 
  <project.build.sourceencoding>utf-8</project.build.sourceencoding> 
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> 
  <java.version>1.8</java.version> 
 </properties> 
 
 <dependencies> 
  <!--开发web项目相关依赖--> 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-web</artifactid> 
  </dependency> 
  <!--springboot单元测试--> 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-test</artifactid> 
   <scope>test</scope> 
  </dependency> 
 </dependencies> 
 
 <!--maven构建--> 
 <build> 
  <plugins> 
   <plugin> 
    <groupid>org.springframework.boot</groupid> 
    <artifactid>spring-boot-maven-plugin</artifactid> 
   </plugin> 
  </plugins> 
 </build> 
</project> 

6.创建一个hellocontroller

package com.example; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
@restcontroller 
public class hellocontroller { 
 @requestmapping("/hello") 
 public string hello() { 
  return "hello,this is a springboot demo"; 
 } 
} 

7.程序自动生成的springbootdemoapplication,会有一个@springbootapplication的注解,这个注解用来标明这个类是程序的入口

package com.example; 
 
import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.springbootapplication; 
 
//入口 
@springbootapplication 
public class springbootdemoapplication { 
 
 public static void main(string[] args) { 
  springapplication.run(springbootdemoapplication.class, args); 
 } 
} 

@springbootapplication开启了spring的组件扫描和springboot的自动配置功能,相当于将以下三个注解组合在了一起

(1)@configuration:表名该类使用基于java的配置,将此类作为配置类

(2)@componentscan:启用注解扫描

(3)@enableautoconfiguration:开启springboot的自动配置功能

8.运行springbootdemoapplication类


测试:

在地址栏中输入http://www.lhsxpumps.com/_localhost:8080/hello


9.使用启动jar包的方式启动

(1)首先进入项目所在目录,如果是mac系统在项目上右键,选择reveal in finder,windows系统在项目上右键选择show in explorer,即可打开项目所在目录

(2)打开终端,进入项目所在目录

cd /users/shanml/ideaprojects/springbootdemo

输入mvn install,构建项目

(3)构建成功后,在项目target文件夹下会多出一个jar包

(4)使用java -jar springbootdemo-0.0.1-snapshot.jar 

启动jar包即可

参考:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网