文章目录
SpringBoot
1. 微服务架构
微服务就是将一个单体架构的应用按业务划分为一个个独立运行的服务. 它们通过HTTP协议进行通信,也可以通过消息中间件进行通信; 服务之间可以采用不同的语言进行编写,可以使用不同的存储技术; 自动化部署减少人为控制,降低出错概率; 随着服务数量的增多,管理起来也越来越复杂,因此采用的是集中化管理方式,如Eureka,Zookeeper. 微服务架构是一种架构风格,一个个大型的复杂的软件应用,有一个或多个微服务构成 系统中的各个微服务可被独立部署,各个微服务之间是松耦合的,每个服务仅关注于自己的任务
SpringBoot是Spring框架的子项目,简化Spring开发
SpringBoot框架中还有两个非常重要的策略,开箱即用和约定优于配置
2. SpringBoot的核心功能
- 起步依赖
起步依赖本质是一个maven项目对象模型(POM工程),定义了对其他库的传递依赖
也就是说起步依赖就是将具备某个功能的jar包打包到一起,提供一些默认依赖
- 自动配置
SpringBoot的自动配置是一个运行时过程,根据一些条件情形,决定Spring应该配置什么东西,该过程自动完成
3. SpringBoot快速入门
- 创建maven工程,选择模板创建Jar工程
- 依赖
<!--继承父类-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.6.RELEASE</version>
</parent>
<groupId>com.zy</groupId>
<artifactId>day101_SpringBoot_Helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<name>day101_SpringBoot_Helloworld</name>
<dependencies>
<!--启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web启动类 tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
- 启动类
//标记为SpringBoot的启动类
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class);
}
}
- 测试类
@SpringBootTest
@RunWith(SpringRunner.class)
/**
*相当于Spring的@RunWith(Junit4Runner.class)和
@Configuration("classpath:appicationContext.xml")
假设使用的是junit5,只需要@SpringBootTest注解
*/
class DemoApplicationTests {
//没有public,说明该版本的springBoot采用的是junit5
@Test
void contextLoads() {
}
}
- 在启动类同级目录下创建controller包
在资源包resource下创建static目录,在static创建hello.html
在浏览器输入http://localhost:8080/user/hello2.do
@Controller
@RequestMapping("/user")
public class HelloController {
@RequestMapping("/hello.do")
public String hello() {
return "/hello.html";
}
}
- 注意事项
JUnit单元测试使用JUnit5中的测试引擎是junit-jupiter-engine
JUnit4使用的测试引擎是junit-vintage-engine
4. Thymaleaf-模板引擎
4.1 入门
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 在resource下创建templates文件夹,并创建html
- 测试-编写html和Controller内容
4.2 语法
- 在html引入名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
- 简单类型值
从后端传到前端的值为简单数据类型及String类型
<h4 th:text="${msg}"></h4>
- 文本信息不转换
后端传递一个a标签到前端
model.addAttribute("uTest","<a href='https://www.baidu.com'>百度一下</a>");
在前端取出
<h4 ><span th:utext="${uTest}"></span></h4>
在页面显示的结果是百度一下
的链接,如果把th:utext改成th:text,得到的结果就是<a href='https://www.baidu.com'>百度一下</a>
文本
- 遍历
后端传递list集合到前端
model.addAttribute("list",list);
前端遍历取值
<span th:each="str:${list}" th:text="${str}"><br></span>
- if
后端传递布尔值,前端取值,if判断
<div>
<span th:if="${result == false}">它是false</span>
<span th:if="${result == true}">它是true</span>
</div>
更多功能可以访问官方文档
MVC自动配置
- 自定义视图解析器
@Bean
public static ViewResolver createViewResolver() {
System.out.println("自定义视图解析器加载了");
return new CustomerViewResolver();
}
private static class CustomerViewResolver implements ViewResolver{
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
修改图标favicon.ico
在配置文件里关闭使用spring默认的图标
spring.mvc.favicon.enabled=false
把自己的favicon.ico放到资源包resources下
Swagger
swagger官网
swagger是一个接口测试工具
准备
- 依赖
<!--springBoot集成Swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 定义swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
- 自定义controller
- 启动服务,访问http://localhost:8080/swagger-ui.html
配置
- Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2);
}
- 在配置类中定义一个以ApiInfo为返回值的方法,在该方法配置信息
private ApiInfo apiInfo() {
Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
return new ApiInfo(
// 标题
"Swagger学习",
// 描述
"学习演示如何配置Swagger",
// 版本
"v1.0",
// 组织链接
"http://terms.service.url/组织链接",
// 联系人信息
contact,
// 许可
"Apach 2.0 许可",
// 许可连接
"许可链接",
// 扩展
new ArrayList<>()
);
}
- Docket 实例关联上 apiInfo()
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
-
重启服务,访问http://localhost:8080/swagger-ui.html
-
构建Docket时通过select()方法配置扫描controller接口
通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zy.controller"))
.build();
}
异步任务
模拟同步任务
- 使用线程模拟业务执行所需要的时
当在浏览器发送请求时,浏览器需要等待5秒才会收到响应"success",在这个期间该页面不能进行操作
@Service
public class AsyncService {
public void asyncTest01() {
try {
Thread.sleep(5000);
}catch (Exception e) {
System.out.println("哈哈哈哈");
}
System.out.println("邮件发送中.......");
}
}
@RestController
public class AsyncController {
@Resource
private AsyncService asyncService;
@RequestMapping("/async.do")
public String asyncTest01() {
asyncService.asyncTest01();
return "success";
}
}
开启异步
在service类上使用@Async注解,该注解可以使用在类上和方法上
在启动类使用@EnableAsync注解
定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式
@Scheduled(cron = "3/5 * 11 17 4 ?")
public void ScheduledTest02() {
System.out.println("每秒钟触发一次");
}
在需要定时任务的方法上打上@Schduled注解,使用表达式表示什么时候触发该方法,还需要在启动类打上@EnableScheduling注解
-
:表示范围
*
:表示匹配该域的任意值
?
:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?
, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
/
:表示起始时间开始触发,然后每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5分钟触发一次
,
:表示列出枚举值,例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
L
:表示最后,只能出现在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
W
:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 。
LW
:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五
#
:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
邮件任务
- 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置文件
spring:
mail:
username: 邮箱
password: 授权码
host: smtp.qq.com
properties:
mail:
smtp:
ssl:
enable: true
@Resource
private JavaMailSenderImpl javaMailSender;
public void MailTest03() {
System.out.println("邮件发送........");
SimpleMailMessage message = new SimpleMailMessage();
message.setSentDate(new Date());
message.setSubject("主题--Java31");
message.setText("内容-天天上课,哭唧唧");
message.setTo("接收人邮箱");
message.setFrom("1132276098@qq.com");
javaMailSender.send(message);
}