SpringBoot是什么:

  SpringBoot是Spring项目中的一个子工程,是一个轻量级框架。

  SpringBoot框架中有两个个非常重要的策略:开箱即用和约定优于配置

一、构建工程

1.开发工具:

  IDEA,JDK1.8以上,Maven3.0+

2代码实现:

  打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点next...

建立好项目结构如下:

AppLication.java:程序的入口

resources:资源文件

  static:放置静态资源,例如css.js等

  templates:模板资源

application.properties:配置文件,在这里我用的properties格式文件,SpringBoot还支持另外一种yml格式的配置文件,在后面会讲到配置文件的使用

pom:依赖文件,这里只说明了几个依赖

!--SpringBoot的起步依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- SpringBoot要继承SpringMvc的controller的开发,导入的web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot的热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

编写Controller内容:

@RestController
public class MyController {
@RequestMapping("/hello")
public String index(){
return "世界";
}
}

注意;@RestController返回的是json数据类型,而@Controlle返回的是Html页面

运行程序主接口Application.java

浏览器访问http://localhost:8080/hello

在这里测试了Springboot在启动的时候注入的bean实例

@SpringBootApplication
public class Demo1Application { public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
} @Bean
public CommandLineRunner commandLineRunner(ApplicationContext applicationContext){
return args -> {
System.out.println("springBoot中出现的bean:");
String[] beanNames = applicationContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String benName:beanNames){
System.out.println(benName);
}
};
}

打印结果如上,大概会出现40-50个bean

Springboot开启单元测试

在src/test下的测试入口

package com.yun.demo1.controllet;

/*SpringBoot单元测试开启*/

import com.yun.demo1.Demo1Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.net.URL; @RunWith(SpringRunner.class) //单元测试
//RANDOM_PORT : 加载一个EmbeddedWebApplicationContext并提供一个真正的servlet环境。
// 嵌入式servlet容器启动并在随机端口上侦听
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestController {
@LocalServerPort
private int port; private URL url; @Autowired
private TestRestTemplate testRestTemplate; @Before
public void setUp() throws Exception {
this.url = new URL("http://localhost:" + port + "/");
}
@Test
public void hello()throws Exception{
ResponseEntity<String> responseEntity = testRestTemplate.getForEntity(url.toString(),String.class);
assertThat(responseEntity.getBody(),equalTo("SpringBoot的测试案例"));
}
}

二、SpringBoot的配置文件详解

  当创建SpringBoot工程时,系统会默认创建一个在src/main/resources目录下创建一个application.properties文件,这个文件就是SpringBoot的配置文件

  propertions格式:    

person.name:=zhangsan

yml文件格式:

person:
name: tom
age: 18

  这里由于个人习惯,我将application.properties改为application.yml文件

  编写controller

@RestController
public class myController {
// @Value:注入Spring boot application.properties配置的属性的值
@Value("${person.name}")
private String name; @Value("${person.age}")
private Integer age; @RequestMapping("/hello")
public String quick(){
return "格尔曼:"+name+":"+age;
}
}

  打印结果如下:

SpringBoot加载自定义配置文件使用@ConfigurationProperties

自义定一个配置文件test.yml

yun:
title: "世界那么大"
desc:"到处去看看"

编写控制器controller,并启动程序

@RestController
@PropertySource(value = "classpath:test.yml")
@ConfigurationProperties(prefix = "yun")
public class myController2 {
@Value("${title}")
private String title;
@Value("${desc}")
private String desc; @RequestMapping("/hexo")
public String quick(){
return title+":"+desc;
}
}

打印结果如下:

 三、Springboot使用模板引擎Thymeleaf

SpringBoot不推荐使用jsp,但是支持一些模板引擎,比如Thymeleaf

Thymeleaf模板引擎的优点:

    动静结合:在有无网络的环境下皆可运行

    开箱即用:提供标准和spring标准两种语言

    多放方言支持:可以快速实现表单绑定,属性编辑器等功能

    与SpringBoot完美整合:SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。

引入依赖

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

Thymeleaf的标准表达式语法

变量表达式

<p>UserName is :  <span th:text="${user.name}">xiaoming</span> !</p>  

URL表达式

<a th:href="@{http://www.thymeleaf.org}">Thymeleaf</a>

其他表达式请看:https://www.jianshu.com/p/908b48b10702

创建一个用户类,并编写controller

@Controller
public class IndexController {
@GetMapping("/index")
public String index(Model model){
List<user> users = new ArrayList<>();
for (int i = 0; i<10; i++){
user u = new user();
u.setId(i);
u.setName("yun"+i);
u.setAddress("世界"+i);
users.add(u);
}
model.addAttribute("users",users);
return "index";
}
}

 在src/tamplates下创建index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>地址</td>
</tr>
<tr th:each="user:${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.address}"></td>
</tr>
</table>
</body>
</html>

 运行程序,打印解结果如下

SpringBoot的学习一:入门篇的更多相关文章

  1. PHP学习笔记 - 入门篇(5)

    PHP学习笔记 - 入门篇(5) 语言结构语句 顺序结构 eg: <?php $shoesPrice = 49; //鞋子单价 $shoesNum = 1; //鞋子数量 $shoesMoney ...

  2. PHP学习笔记 - 入门篇(4)

    PHP学习笔记 - 入门篇(4) 什么是运算符 PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. PHP中的算术运算符 算术运算符主要 ...

  3. PHP学习笔记 - 入门篇(3)

    PHP学习笔记 - 入门篇(3) 常量 什么是常量 什么是常量?常量可以理解为值不变的量(如圆周率):或者是常量值被定义后,在脚本的其他任何地方都不可以被改变.PHP中的常量分为自定义常量和系统常量 ...

  4. PHP学习笔记--入门篇

    PHP学习笔记--入门篇 一.Echo语句 1.格式 echo是PHP中的输出语句,可以把字符串输出(字符串用双引号括起来) 如下代码 <?php echo "Hello world! ...

  5. netty深入学习之一: 入门篇

    netty深入学习之一: 入门篇 本文代码下载: http://download.csdn.net/detail/cheungmine/8497549 1)Netty是什么 Netty是Java NI ...

  6. Java工程师学习指南 入门篇

    Java工程师学习指南 入门篇 最近有很多小伙伴来问我,Java小白如何入门,如何安排学习路线,每一步应该怎么走比较好.原本我以为之前的几篇文章已经可以解决大家的问题了,其实不然,因为我之前写的文章都 ...

  7. Elasticsearch学习记录(入门篇)

    Elasticsearch学习记录(入门篇) 1. Elasticsearch的请求与结果 请求结构 curl -X<VERB> '<PROTOCOL>://<HOST& ...

  8. PHP学习笔记 - 入门篇(2)

    PHP入门篇(2) 什么是变量 变量是用于存储值的容器,如下 $var = @"6666" 如何定义变量 定义变量就是像服务器的内存申请空间,用来存储数据,eg: <?php ...

  9. RabbitMq学习一入门篇(hello world)

    简介 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java,也是众多消息队列中表现不俗的一员,作用就是提高系统的并发 ...

  10. labview学习_入门篇(一)

    写在前面的话: 在上大学的时候,实验室的老师推荐用labview工具编写上位机软件,当时不想用labview,感觉不写代码心里不踏实,后来用vb和matalb开发了上位机软件.但现在由于部门的几款工具 ...

随机推荐

  1. 借助meta影藏顶部菜单

    1===>报错 Cannot find module 'webpack/lib/Chunk' 删除node_modules 然后重新下载 4==> 现在已进入页面,底部就有四个菜单,在点击 ...

  2. 批量执行用例,且结果显示在html中,python控制台也会显示运行过程

    下载HTMLTestRunner.py放到python安装目录的Lib下: 如下以登录为例,创建不同的.py文件, ========================================== ...

  3. 让人又爱又恨的this

    this是个神奇的东西, 既可以帮助我们把模拟的类实例化. 又可以在事件绑定里准确指向触发元素. 还可以帮助我们在对象方法中操作对象的其他属性或方法. 甚至可以在使用apply.call.bing.f ...

  4. mac使用技巧和快捷键

    mac快捷键 shift+command+delete 清除缓存 shift+command+. 查看隐藏文件 ctrl+空格 切换输入法 shift+option+F11/F12 精细调节声音 fn ...

  5. 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)

    \(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...

  6. 《一起学netty》

    o文章摘自 netty 官网(netty.io)   netty 是一个异步的,事件驱动的网络应用通信框架,可以让我们快速编写可靠,高性能,高可扩展的服务端和客户端   样例一:discard ser ...

  7. http状态码大全(404、505、502,500)

    HTTP 400 – 请求无效HTTP 401.1 – 未授权:登录失败HTTP 401.2 – 未授权:服务器配置问题导致登录失败HTTP 401.3 – ACL 禁止访问资源HTTP 401.4 ...

  8. h5中的audio音频标签与对应的dom对象

    HTML5中提供了一个<audio>标签,同时也提供了一个Audio对象给JavaScript去操作,这个对象属于HTML的DOM对象. H5中的<audio>标签 此标签是H ...

  9. Spring Cloud 新一代Web框架微服务翘楚(一)

    序言 springcloud是微服务架构的集大成者,将一系列优秀的组件进行了整合.基于springboot构建,对我们熟悉spring的程序员来说,上手比较容易. 通过一些简单的注解,我们就可以快速的 ...

  10. Java 函数式编程—@FunctionalInterface----functional interface

    单一函数接口,可以使用拉姆达表达式的形式具体化和实例化. 本质是将接口函数签名化. 如定义了一个函数式接口如下: @FunctionalInterface interface GreetingServ ...