spring boot(一)--起步

一、简介

spring boot简介化了java项目的开发,这里将对spring boot进行初步介绍,并给出一个实例。

二、spring boot优点

1、可以以单独的jar包启动,即 java -jar xx.jar  ;

2、可以选择内嵌servlet容器(如tomcat、jetty等),这样就不用再打成war部署运行;

3、提供了很多start pom简化了maven的依赖,通过一个依赖配置,可以把开发所需的依赖集合全都添加进来;

4、提供了http、ssh、telent方式对运行的项目进行监控;

5、提倡使用java注解的方式进行配置,整洁干净;

三、借助IDE(intellij idea)新建项目步骤

这里开发工具使用的是intellij idea,jdk版本使用的是java1.8。

1、进入新建项目页

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

左侧选择Spring Initializr,右侧中choose Initializr Service URL选择default  :https://start.spring.io

2、配置项目名、group、artifact等信息 

3、选择项目使用的技术

4、配置模块名及路径

5、添加controller及在主启动类配置包路径

至此,项目新建完成。运行主启动类,项目即启动了。

四、手动创建项目步骤

这里以创建web项目为例

1、新建maven项目

2、pom.xml文件配置

a)在pom.xml添加spring boot的父级依赖

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.0.BUILD-SNAPSHOT</version>
  <relativePath/>
</parent>

b)在pom.xml添加支持web的starter pom

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

c)在pom.xml添加spring boot的编译插件

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

完整的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.dragon.springbootselfstudy</groupId>
  <artifactId>springbootselfstudy</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring boot study</name>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.BUILD-SNAPSHOT</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <finalName>spring boot study</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

3、添加controller

package com.dragon.springbootselfstudy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {
    @RequestMapping("/")
    @ResponseBody
    public String index() {
        return "hello word!";
    }
}

4、使用注解@SpringBootApplication定义运行类SpringbootMain.java

package com.dragon.springbootselfstudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.dragon.springbootselfstudy")
public class SpringbootMain {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMain.class, args);
    }
}

运行SpringbootMain.java中的main方法,项目即启动了。

在浏览器输入:http://127.0.0.1:8080/

即可看到输入 :hello word!

猜你喜欢

转载自blog.csdn.net/chinabestchina/article/details/81142510