前言

此文只是记录自己简单学习spring boot的笔记。所以,文章很多只是初步理解,可能存在严重错误。

一、Spring boot的初步理解

1、spring boot的目标 (摘自:spring-boot参考指南 official 中文

  • 为所有的Spring开发提供一个从根本上更快的和广泛使用的入门经验。
  • 开箱即用,但你可以通过不采用默认设置来摆脱这种方式。
  • 提供一系列大型项目常用的非功能性特征(比如,内嵌服务器,安全,指标,健康检测,外部化配置)。
  • 绝对不需要代码生成及XML配置。

2、spring boot代替了什么,Spring、SpringMVC/Struts2、hibernate/Mybatis?

个人理解:代替了spring。用代替/取代来解释貌似都不好,更准确的可能是封装了spring,使搭建SSH/SSM更快捷。

传统的spring有很多xml配置,例如:dataSource、transactionManager、AOP、bean等等xml的配置。即便用注解,也要在xml中配置component-scan等。

但在spring boot,遵循“约定大于配置”,所以尽可能的避免了xml配置。

3、spring boot避免了哪些xml?

主要是spring的application.xml、context等xml。但例如:mybatis.xml、log4j.xml等还是要的。

4、什么是Starter POMs?

Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合。你可以获取所有Spring及相关技术的一站式服务,

而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符。例如,如果你想使用Spring和JPA进行数据库访问,

只需要在你的项目中包含 spring-boot-starter-data-jpa 依赖,然后你就可以开始了。

参考参考指南13.4,pdf页数56/420。

二、Sping boot的建议

1、推荐使用jdk1.8+。(虽然可以在1.6-1.7中使用)

2、servlet容器。

3、Spring Boot兼容Apache Maven 3.2或更高版本;

Spring Boot兼容Gradle 1.12或更高版本。

4、建议使用thymeleaf、velocity、beetl等模版引擎。而不建议使用jsp(虽然可以配置jsp支持)。

三、demo

1、代码结构(idea)

2、解释

application.java:入口

GreetingController.java:controller层代码。

index.html:默认首页。localhost:8080访问的页面即index.html。

greeting.html:请求页面。

(1、为什么html是在resources中。2、为什么结构是static、templates。3、如何修改此“约定”,把html放到webapp/WEB-INF下。

这些问题在以后学习,此文不讨论。)

3、source code

<?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>VergiLyn</groupId>
<artifactId>Spring boot</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!-- 模版框架thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot 单元测试依赖 BEGIN-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot 单元测试依赖 END-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

pom.xml

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Controller
//@RestController
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="VergiLyn") String name
, Model model) {
model.addAttribute("name", name);
return "greeting";
} }

index.html

<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>

greeting.xml

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

spring boot的单元测试模版

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestOfficialDemo { @LocalServerPort
private int port; @Autowired
private TestRestTemplate restTemplate; @Test
public void testGreetingDefault() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/greeting",String.class)) .contains("VergiLyn");
}
}

4、测试

运行application.java的main()方法,控制台打印:

请求访问localhost:8080

点击超链接“here”:可以看到用的是defaultValue。

自定义请求greeting:localhost:8080/greeting?name=VergiLyn淡无欲

四、部分解释

1、@Controller、@RestController、@RequestMapping、@RequestParam是spring的注解,而非spring boot提供的注解。

2、@Controller与@RestController的区别

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
String value() default "";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
String value() default "";
}

通过sourceCode的定义可以看出:@RestController在@Controller的基础上多了@ResponseBody。简单理解就是:@RestController不能返回html、jsp。

(Rest即webservice中RESTful的rest,可以理解成@RestController主要用来返回json等数据,而非页面)

3、@SpringBootApplication

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
Class<?>[] exclude() default {}; String[] excludeName() default {}; @AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {}; @AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}

摘自:参考指南

很多Spring Boot开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。由于这些
注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。
  该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。

附录

玩转spring boot——快速开始

Serving Web Content with Spring MVC (官方demo)

spring-boot参考指南 official 中文

【spring boot】SpringBoot初学(1) - Hello World的更多相关文章

  1. Spring Boot]SpringBoot四大神器之Actuator

    论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...

  2. Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件

    1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...

  3. spring boot 引导

    链接:https://www.zhihu.com/question/39483566/answer/243413600 Spring Boot 的优点快速开发,特别适合构建微服务系统,另外给我们封装了 ...

  4. spring boot 修改banner

    在resources目录下新建一个banner.txt,里面添加要显示的内容,如: ////////////////////////////////////////////////////////// ...

  5. 学习 Spring Boot 知识看这一篇就够了

    从2016年因为工作原因开始研究 Spring Boot ,先后写了很多关于 Spring Boot 的文章,发表在技术社区.我的博客和我的公号内.粗略的统计了一下总共的文章加起来大概有六十多篇了,其 ...

  6. 1. Spring Boot入门

    1.Spring Boot简介 简化Spring应用开发的一个框架 整个Spring技术栈的一个大整合 J2EE开发的一站式解决方案 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 ...

  7. 阿里面试官:小伙子,给我说一下Spring 和 Spring Boot 的区别吧

    前言 对于 Spring和 SpringBoot到底有什么区别,我听到了很多答案,刚开始迈入学习 SpringBoot的我当时也是一头雾水,随着经验的积累.我慢慢理解了这两个框架到底有什么区别,相信对 ...

  8. 关于Spring Boot的博客集合

    掘金: 关于Spring Boot的博客集合 CSDN: Spring Boot教程 掘金: SpringBoot2 简书: Spring Boot 核心技术 天码营 Spring Data JPA: ...

  9. Spring Boot入门学习

    1. Spring Boot概述 1.1.什么是Spring Boot SpringBoot是一个可使用Java构建微服务的微框架.是Spring框架及其社区对"约定优先于配置"理 ...

  10. 【spring boot】SpringBoot初学(2.1) - properties读取明细

    前言 算是对<SpringBoot初学(2) - properties配置和读取>的总结吧. 概念性总结 一.Spring Boot允许外化(externalize)你的配置.可以使用pr ...

随机推荐

  1. CCF_201503-2_数字排序

    自己写个排序的cmp. #include<iostream> #include<cstdio> #include<algorithm> using namespac ...

  2. 第一次安装android studio遇到的问题

    安装android studio一点都不顺利,最后总是成功安装,但是忘了把问题记录下来,下一次肯定还肯能出现问题 忘了把问题记录下来了,好像是sync failed 第一次安装3.1.4遇到的问题,好 ...

  3. Codeforces 1304E 1-Trees and Queries (树上距离+思维)(翻译向)

    题意 给你一棵树,q个询问(x,y,a,b,k),每次问你如果在(x,y)加一条边,那么a到b能不能走k步,同一个点可以走多次 思路(翻译题解) 对于一条a到b的最短路径x,可以通过左右横跳的方法把他 ...

  4. Vsftpd: 基于PAM认证的虚拟用户和匿名用户

    目录 环境说明效果说明及截图①. 安装组件②. 系统账户建立③. 编辑vsftpd的配置文件④. 生成虚拟用户的数据库文件⑤. 生成一个使用vsftpd_login.db数据文件的PAM认证文件⑥. ...

  5. nginx设置域名转发到指定端口

    1.修改nginx.conf文件,将worker_processes  1 修改为  worker_processes  auto 2.创建端口代理配置文件(域名地址如:xx.baidu.com , ...

  6. Algorithms - Insertion Sort - 插入排序

    Insertion Sort - 插入排序 插入排序算法的 '时间复杂度' 是输入规模的二次函数, 深度抽象后表示为, n 的二次方. import time, random F = 0 alist ...

  7. 《Head first设计模式》学习笔记

    1. 单例模式 2. 工厂模式 3. 抽象工厂 4. 策略模式 5. 观察者模式 6. 装饰者模式 7. 命令模式 8. 适配器模式 9. 外观模式 10. 模版方法模式 11. 迭代器模式 设计模式 ...

  8. Linux ftp VSftp

    一.Linux FTP服务器分类: <1>wu-ftp <2>proftp=profession ftp <3>vsftp=very security ftp  本 ...

  9. 【转载】Java的Vector,ArrayList,LinkedList

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  10. 【转载】s19文件格式详解

    来源:http://blog.csdn.net/xxxl/article/details/19494187 1.概述 为了在不同的计算机平台之间传输程序代码和数据,摩托罗拉将程序和数据文件以一种可打印 ...