SpringBoot:创建第一个SpringBoot简单应用

1 、SpringBoot介绍:

 Spring 团队在现有Spring框架的基础上发布了一个创新的主要框架:Spring Boot。开发Spring Boot的主要动机是简化配置和部署spring应用程序的过程。使用Spring Boot将能够以更灵活的方式开发Spring应用程序,并且能够通过最小配置Spring,避免一些繁琐的开发步骤和样板代码和配置,而更专注于解决应用程序的功能需求。

 

2、项目创建

 创建一个maven项目(前提:Java环境、Maven 都搭建好):

 

 填写Group Id,Artifact Id ,选择pakaging。

 

3、 添加依赖

创建项目成功后,配置pom.xml:

扫描二维码关注公众号,回复: 1324625 查看本文章

<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>SpringBootProject</groupId>

  <artifactId>FirstSpringBootProject</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

   <properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <failOnMissingWebXml>false</failOnMissingWebXml>

    <start-class>com.springboot.start.applicationStart</start-class> 

  </properties>

  <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.2.RELEASE</version>

    <relativePath />

</parent>

 <dependencies>

    <!-- 上边引入 parent,因此 下边无需指定版本 -->

    <!-- 包含 mvc 等jar资源 -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>  

    </dependency>

      <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-test</artifactId>

         <scope>test</scope>

      </dependency>

       <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-aop</artifactId>

      </dependency>

         <!-- 添加servlet-api的依赖 -->

      <dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>javax.servlet-api</artifactId>

  <scope>provided</scope>

</dependency>

 </dependencies>

<build>

    <plugins>

        <plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

     </plugins>

    </build>

</project>

 

4、创建控制类、启动类

创建 com.first.springboot 包,并在该包下创建一个 Controller 类,如下:

之后写一个firstspringboot方法:

 

package com.first.springboot;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

 

@RestController

public class SpringStartController {

   @GetMapping("/firstspringboot")

   public String firstspringboot()

   {

      return "Your First SpringBoot Project";

   }

 

}

再创建一个启动类:

 package com.springboot.start;

 import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 @SpringBootApplication

public class applicationStart {

    public static void main(String[] args)

 {

        SpringApplication.run(applicationStart.class, args);}

}

 

5、启动项目

Build 项目:右键Run As-->Maven install,成功后启动项目:在启动项目类applicationStart处,右键  Run As-->java application ,当出现Tomcat started on port(s): 8080 (http) ,说明启动成功,在浏览器上输入: http://localhost:8080//firstspringboot,

 至此,恭喜你,你的第一个SpringBoot应用创建成功了。


 

猜你喜欢

转载自www.cnblogs.com/jiyuyun/p/9120856.html