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. emacs 帮助相关命令

    emacs 帮助相关命令 如下表: No. 键盘操作 键盘操作对应的函数 回答的问题 01 ctrl-h c describe-key-briefly 这个按键组合将运行哪个函数 02 ctrl-h ...

  2. protocol buffers 使用方法

    protocol buffers 使用方法 为什么使用 Protocol Buffers 我们接下来要使用的例子是一个非常简单的"地址簿"应用程序,它能从文件中读取联系人详细信息. ...

  3. CountDownLatch/CyclicBarrier/Semaphore 使用过吗?

    CountDownLatch/CyclicBarrier/Semaphore 使用过吗?下面详细介绍用法: 一,(等待多线程完成的)CountDownLatch  背景; countDownLatch ...

  4. jieba、NLTK学习笔记

    中文分词 - jiebaimport re import jieba news_CN = ''' 央视315晚会曝光湖北省知名的神丹牌.莲田牌“土鸡蛋”实为普通鸡蛋冒充,同时在商标上玩猫腻, 分别注册 ...

  5. C++ 模板特化、偏特化测试程序

    #include <iostream> // 偏特化的模板不会自己添加构造函数 ctor 和 析构函数 dtor #if 1 // P1 template <typename T1, ...

  6. 消息中间件(二)MQ使用场景

    一.消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,Rabbit ...

  7. 用pip命令把python包安装到指定目录

    sudo pip install transforms3d --target=/usr/local/lib/python2.7/site-packages pip install transforms ...

  8. 20191028 Codeforces Round #534 (Div. 1) - Virtual Participation

    菜是原罪. 英语不好更是原罪. \(\mathrm{A - Grid game}\) 题解 \(4 \times 4\) 的格子,两种放法. 发现这两种在一起时候很讨厌,于是强行拆分这个格子 上面 \ ...

  9. 如果对象的引用被置为null,;垃圾回收器是否会立即释放对象占用的内存?

    不会,在下一个垃圾回调周期中,这个对象将是被可回收的. 也就是说并不会立即被垃圾收集器立刻回收,而是在下一次垃圾回收时才会释放其占用的内存.

  10. 在Ubuntu18.04.2LTS上安装搜狗输入法

    在Ubuntu18.04.2LTS上安装搜狗输入法 一.前言 最近项目使用到了Linux系统,因此就安装了Ubuntu18.04.2这个最新的LTS的OS.整体的使用效果是不敢恭维的,特别是使用虚拟机 ...