千字图文带你搞定Spring Boot核心知识点

> 文章已收录 [架构技术专栏](http://47.104.79.116:4321/)  收藏不迷路,点击获取更多视频资料福利
 

最近新的项目架构启用spring boot cloud,SO现在先坐下简单的技术梳理,后边的博客会把spring的技术细节,boot的技术细节重新梳理一遍

 

 

1、下面是根据条件初始化bean

 

 

 


2、读取配置信息操作

加载配置可以用@PropertySource("classpath:com/ecej/test2/test.properties") 记得要用 private Environment environment; 读取配置

 

package com.ecej.test2;

import org.apache.commons.io.IOUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import org.springframework.core.env.Environment;

import org.springframework.core.io.Resource;

@Configuration

@ComponentScan("com.ecej.test2")

@PropertySource("classpath:com/ecej/test2/test.properties")

public class ELConfig {

@Value("I LOVE YOU")

private String normal;

@Value("#{systemProperties['os.name']}")

private String osName;

@Value("#{T(java.lang.Math).random() * 100.0}")

private double randomNumber;

@Value("#{demoService.another}")

private String fromAnother;

@Value("#classpath:com/ecej/test2/test.properties")

private Resource testFile;

@Value("http://www.baidu.com")

private Resource testUrl;

@Value("${book.name}")

private String bookName;

@Autowired

private Environment environment;

@Bean

public static PropertySourcesPlaceholderConfigurer propertyConfigure() {

return new PropertySourcesPlaceholderConfigurer();

}

public void outputResource() {

try {

System.out.println(normal);

System.out.println(osName);

System.out.println(randomNumber);

System.out.println(fromAnother);

System.out.println(IOUtils.toString(testUrl.getInputStream()));

System.out.println(bookName);

System.out.println(environment.getProperty("book.author"));

} catch (Exception e) {

// TODO: handle exception

}

}

}

 

 

 

 

 


3、spring event

事件

1 、首先我们要实现ApplicationListener 实现我们自己的监听

2、 定义我们自己的事件 通过集成ApplicationEvent实现

3、 定义config启动

通过applicationContext 发布事件

 

@Component

public class DemoPublisher {

@Autowired

private ApplicationContext applicationContext;

public void publish(String msg) {

applicationContext.publishEvent(new DemoEvent(this, msg));

}

}

 

 

 

 

 


4、spring Aware

 

讲解:bean 和spring是无耦合的,但是如果想用到spring容器的功能资源,就要你的bean知道spring的存在,这就是spring aware

 

 

 

 


5、多线程

 

spring通过 TaskExecutor来实现多线程并发编程。使用ThreadPoolExecutor可实现基于线程池的TaskExecutor,使用@EnableAsync开启对异步任务的支持,并通过在实际执行bean方法中使用@Async注解来声明一个异步任务


6、计划任务

 

通过配置注解@EnableScheduline来开启对计划任务的支持,然后再要执行的任务上加注解@Scheduled

spring通过@Scheduled支持多种类型计划任务,包含cron,fixDelay、fixRate(固定时间执行)


7、条件注解@Conditional

 


8、组合注解与源注解

 

源注解:就是可以注解到别的注解上的注解,被注解的注解称之为组合注解。组合注解有源注解的功能。

@Configuration  @ComponentScan 组合出 Wiselyconfiguration


9、@Enable*注解工作原理

 

 


10、Spring MVC

 

下面所讲解的都是配置在MyMVCConfig

通过实现WebApplicationInitializer 等同于web.xml配置

基本配置

spring MVC的定制配置需要我们配置集成一个WebMvcConfigurerAdapter,并在此使用@EnableWebMvc注解,来开启对spring MVC配置支持

静态资源

重写addResourceHandlers方法实现

 

拦截器配置

 

类似servlet的Filter

 

可以让普通bean 实现HanlderIntercepetor接口或者继承HandlerInterceptorAdapter类来实现自定义拦截器

在boot中通过重写WebMvcConfigurerAdapter 的 addInterceptors方法来注册自定义拦截器

@ControllerAdvice

 

@ExceptionHandler定义全局处理 ,通过value属性可以设置拦截过滤条件

在开发中经常会遇到跳转页面的事情,我们还要单独写一个方法很麻烦,现在可以这样

 

 

 

路径参数配置

 

在spring mvc中路径参数如果带点“.” ,那点后面的值将被忽略。通过重写configurePathMatch(PathMatchConfigurer) 可不忽略 点后参数

 

 

 

文件上传

 

demo集合

 


二 、正式开始spring boot

 

 

 

CLI  命令行控制工具


1、@SpringBootApplication 和入口

 

这个标签是个组合注解,包含了@Configuration @EnableAutoConfiguration @ComponentScan三个标签

@EnableAutoConfiguration 让spring boot根据类路径中的jar包依赖为当前项目进行自动配置

在spring boot中我们可以使用

@Value("${book.author}")直接注入属性,但是还是感觉一个个注入麻烦啊,SO,我们可以直接映射一个类,用@ConfigurationProperties(prefix="author",locations={"classpath:author.properties"})通过prefix指定前缀,通过locations指定位置


2、spring boot 的web开发

 

需要定义模版信息的话,使用ViewResolver  ,别忘了在config上加注解@EnableWebMvc

 

@Bean

public InternalResourceViewResolver viewResolver() {

InternalResourceViewResolver resolver = new InternalResourceViewResolver();

resolver.setPrefix("WEB-INF/classes/views/");

resolver.setSuffix(".jsp");

resolver.setViewClass(JstlView.class);

return resolver;

}

 

 

 

 

 

静态资源默认放在src/main.resources/static 下面


3、静态首页的支持

 


4、接管spring boot 的web配置

 

如果boot 提供的配置不是我们需要的,可以通过配置类修改,

注解来实现自己完全控制

@Configuration

@ComponentScan("com.ecej.test.mvc")

@EnableWebMvc

如果我们想即用默认配置,又增加自定义配置,可以集成WebMvcConfigurerAdapter,无需使用@EnableWebMvc注解


5、注册servlet Filter Listener(重点来了)

可以注册ServletRegistrationBean  FilterRegistrationBean  ServletListenerRegistrationBean 的bean来实现


6、Tomcat配置

 

其实就是servlet容器配置,因为BOOT内置的是tomcat,所以也就叫tomcat配置了

配置都在org.springframework.boot.autoconfigure.web.ServerProperties 中(其实大部分都有这么个配置)

 

SO,我们只需要在application.properties中配置就好了(如果你想拥别的名字,只需要配置下就行咯,在上边有提到过)。通用的配置都以server作为前缀

例子:

配置容器

server.port=8080

server.session-timeout=2

配置tomcat

server.tomcat.uri-encoding=UTF-8

上边都是配置文件配置,如果想玩玩代码也是可以的,下面介绍代码配置

想配置servlet容器可以实现一个EmbeddedServletContainerCustomizer的接口(注意声明的类要为static)

想直接配置tomcat等则可以直接定义TomcatEmbeddedServletContainerFactory等

 


7、SSL配置(安全套接层)

 

生成证书:

JDK下有工具keytool ,他是一个证书管理工具,可以用来生成证书

 

在你命令执行的当前目录下生成了一个.keystore的证书

配置我们的配置文件

server.port=8000

server.ssl.key-store=.keystore

server.ssl.key-store-password=123456

server.ssl.keyStoreType=JKS

server.ssl.keyAlias:tomcat

此时在启动项目就变成了https的


8、设置自己的Favicon

 

这个就非常简单了,只需要将自己的favicon.ico文件放在META-INF/resources/   resources/  static/  public/下面任意一个目录下就行了。


9、WebSocket

 

这到底是个什么鬼呢?

官方说法,就是为浏览器和服务端提供双工异步通信的功能。直接使用WebSocket会使开发非常繁琐的,所以我们使用它的子协议STOMP,它是一个更高级的协议,STOMP协议使用一个基于帧的格式来定义消息,与HTTP的request response类似。

spring boot内置了这玩意,可以看websocket包下的类

需要加入

spring-boot-starter-websocket 包

@EnableWebSocketMessageBroker注解并继承AbstractWebSocketMessageBrokerConfigurer

模式:

广播式  会将消息发送给所有连接了当前的浏览器

代码片段

 

package com.ecej.demo2.websocket;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.messaging.simp.config.MessageBrokerRegistry;

import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;

import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;

import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration

@ComponentScan(value = "com.ecej.demo2.websocket")

@EnableWebSocketMessageBroker

public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override

public void registerStompEndpoints(StompEndpointRegistry register) {

register.addEndpoint("/endpointWisely").withSockJS();

}

@Override

public void configureMessageBroker(MessageBrokerRegistry register) {

register.enableSimpleBroker("/topic");

}

}

 

 

 

 

 

解析:

1、通过@EnableWebSocketMessageBroker 注解开启使用STOMP协议传输基于代理(message broker)的消息,

这时控制器支持使用@MessageMapping,就像使用@RequestMapping一样

2、注册STOMP协议的节点,并映射的指定的URL

3、注册一个STOMP的endpoint,并指定使用SocketJS协议

4、配置消息代理(message broker)

5、广播式应配置一个/topic消息代理


10、spring 的事物机制

 

spring事物机制提供了一个PlatformTransactionManager接口,不同的数据访问技术的事物使用不同的接口实现

 

声明式事物

使用@Transactional注解在方法上表明该方法需要事物支持,基于AOP实现操作。

使用@EnableTransactionManagement来开启上边的注解

类级别的注解@Transactional会重载方法级别的

 


11、缓存支持

 

不同的缓存技术,需要不同的cacheManager

 


12、异步消息

 

spring 对JMS和AMQP的支持分别来自于spring-jms 和spring-rabbit

 

他们分布需要ConnectionFactory来实现连接消息代理,并分别提供了JmsTemplate、RabbitTemplate

spring为JMS 、AMQP提供了@JmsListener @RabbitListener 注解在方法上监听消息代理发布的消息。我们只需要分别通过@EnableJms @EnableRabbit开启支持

 

 

专注于分享技术干货文章的地方,内容涵盖java基础、中间件、分布式、apm监控方案、异常问题定位等技术栈。多年基础架构经验,擅长基础组件研发,分布式监控系统,热爱技术,热爱分享

 

  • 11
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring Boot与Web开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

架构技术专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值