Sping Boot Start

 

一、Introducing Spring Boot

Spring Boot使创建可运行的独立的、基于Spring的产品级应用程序变得很容易。只需要很少的Spring配置。可以通过使用Java -jar或更传统的war部署来启动的Java应用程序。还提供了一个运行“spring脚本”的命令行工具。

二、System Requirements

Spring Boot 2.1.0.RELEASE 需要:

Java 8 + 。

Spring Framework 5.1.2.RELEASE及以上.  。

Maven 3.3+ 或 Gradle 4.4+ 。

Tomcat9.0。

servlet4.0。

三、Hello World!

1.创建一个maven quickstart 

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.thundersoft</groupId>
  <artifactId>spingBoot</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spingBoot</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <!--父依赖-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!--打成war包-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

cmd进入项目的目录执行 mvn package 命令 看是否打包成功。

运行mvn dependency:tree查看依赖。

2.编写测试类:

package com.thundersoft;

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;

/**
 * Created by XXX on 2018/11/10.
 */
@EnableAutoConfiguration
@RestController
public class Example {

    @RequestMapping("/")
    String hello() {
        return "Hello World!";
    }

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

①可以直接run main 方法

②也可cmd进入项目目录输入命令 mvn spring-boot:run

③cmd 进入项目目录输入mvn package (此时可以输入 jar tvf  spingBoot-1.0-SNAPSHOT.jar  查看依赖) 然后 输入 java -jar   target/spingBoot-1.0-SNAPSHOT.jar  (看自己target目录下是什么名)

打开浏览器输入localhost:8080

返回 Hello World!成功

/* 通过命令结束(Ctrl+c) */

@RestController and @RequestMapping 是Spring MVC 的注解接收请求。

@EnableAutoConfiguration。这个注释告诉Spring Boot根据您添加的jar依赖关系“猜测”您希望如何配置Spring。由于Spring -boot-starter-web添加了Tomcat和Spring MVC,因此自动配置假定您正在开发web应用程序并相应地设置Spring

注意:

Spring Boot的每个版本都提供了它所支持的托管依赖性列表。实际上,您不需要为配置中的任何依赖项提供一个版本,SpringBoot帮你管理。当您升级SpringBoot本身时,这些依赖项也会以一致的方式升级。

如果需要,您仍然可以指定一个版本并覆盖Spring Boot的建议。

<properties>

.....

</properties>

Spring Boot的每个版本都与Spring框架的基本版本相关联。强烈建议不要指定它的版本。

spring-boot-starter-parent 可以识别application.properties和application.yml文件。

 

上面那种写法只适合在一个类中,下面介绍另一种

entity 和controller

package com.thundersoft.entity;

/**
 * Created by XXX on 2018/11/10.
 */
public class User {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.thundersoft.controller;

import com.thundersoft.entity.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by XXX on 2018/11/10.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/{age}")
    public User getUser(@PathVariable("age") int age) {
        User user = new User();
        user.setAge(age);
        user.setName("Tom");
        return user;
    }
}

启动类:

package com.thundersoft;

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

/**
 * Created by XXX on 2018/11/10.
 */
//@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

可以只使用@SpringBootApplication也可使用@EnableAutoConfiguration 加 @ComponentScan 

注意的是启动类放的位置要放在entity和controller包外面。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值