SpringBoot

本文深入探讨了SpringBoot的核心特性,包括自动配置、启动器原理、MVC配置。详细介绍了如何创建第一个SpringBoot程序,理解其配置和Web开发过程,如静态资源处理、首页定制、模板引擎Thymeleaf的使用。此外,文章还涉及了springSecurity、Shiro的安全认证和授权,以及Swagger的API文档配置。最后,讨论了微服务、分布式系统、Dubbo与Zookeeper的集成,并展望了服务网格的发展趋势。
摘要由CSDN通过智能技术生成

SpringBoot

javaSE:OOP

mysql:持久化

html+css+js+jquery+框架:视图,框架不熟练,css不好

javaweb:独立开发mvc三层架构

ssm:框架,简化了我们的开发流程,配置较为复杂

​ spring+springMVC+mybatis

​ spring:主要是ioc与aop技术,大大简化了配置信息的配置难度。

​ springMVC:主要是方便了前端发送请求到后端接收方面的构建难度

​ -----------------原本:我们需要让每个servlet继承HttpServlet,并且需要在 web.xml中手动注册每个servlet从 而实现消息的转化

​ -----------------springMVC:我们需要servlet继承Controller类,在web.xml中只需要注册核心的 dispatchServlet,在bean文件中注册servlet(可以用注解代替),其中包括json乱码、过滤器、拦截器等都可以通过在bean.xml中配置

spring与springMVC的区别:spring主要注重于后端结构的优化、springMVC主要注重于前后端交互模块 上使用spring来简化代码结构

war包:tomcat运行

spring再简化:springboot-jar包:内嵌tomcat,微服务架构!

服务越来越多:springcloud

新服务架构:服务网格!

maven、spring、springmvc、springboot…:约定大于配置

程序 = 数据结构 + 算法

**微服务:**是一种风格,架构风格

MVC三层架构 : MVVM 微服务架构

业务:service:

第一个springBoot程序

  • jdk1.8
  • maven3.6.1
  • springboot:最新版
  • idea

官方:提供了一个快速生成的网站!idea集成了这个网站

  • 可以在官网下载
  • 直接用idea创建一个springboot项目(一般开发直接在idea中创建)

原理初探

pom.xml

  • springboot-dependencies:核心依赖在父工程中
  • 我们在写或者引入springboot依赖的时候不需要指定版本,就是因为有这些版本仓库

启动器

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

我们要使用什么功能,就只需要找到对应的启动器就好了(例如:spring-boot-starter-web)

主程序

//本身就是一个spring组件
//程序的主入口 SpringBootApplication:标注这个类是一个springboot的应用
@SpringBootApplication
public class HelloWorldApplication {
   public static void main(String[] args) {
      //将springboot应用启动
      SpringApplication.run(HelloWorldApplication.class, args);
   }
}

自动配置:@SpringBootApplication

​ 注解详解

@SpringBootConfiguration:springboot的配置
	@Configuration:spring配置类
		@Component:spring组件


@EnableAutoConfiguration:自动配置
	@AutoConfigurationPackage:自动配置包
		@Import({Registrar.class}):导入选择器“包注册”
	@Import({AutoConfigurationImportSelector.class}):自动配置导入选择
	
	
//获取所有的配置
 List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes)
获取候选的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

META-INF/spring.factories:自动配置的核心文件

在这里插入图片描述

结论:所有的spring自动配置类都在启动类被扫描并加载—spring.factories文件

​ spring.factories文件包含所有自动配置类()

  1. springboot在启动的时候从类路径下/META-INF\spring.factories获取指定的值
  2. 将这些自动配置类导入容器,自动配置生效,帮我们进行自动配置
  3. 整个javaEE,解决方案和自动配置的都整合在spring-boot-autoconfigure-2.6.0-20211102.142727-409.jar包中
  4. 他会把所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器
  5. 容器中有许多xxxxAutoConfiguration的文件,就是这些类给容器中导入这个场景所需的组件,并自动配置(@Configuration)
  6. 有了自动配置类,免去了我们手动编写配置文件的工作

启动

javaConfig @Configuration @Bean

关于springboot,谈谈你的理解

  1. 自动装配
  2. run():判断是否为web项目、推断主类、配置监听器、

全面接管springMVC的配置!实操!

springboot配置

在这里插入图片描述

yaml可以直接给实体类赋值

在配置文件中能配置的东西,都存在一个固定的规律

配置文件 spring.xxx= value ==》 xxxxProperties ==》ConfigrationProperties(prefix=“xxx”)

在这里插入图片描述

springBoot Web开发

自动装配

  1. 创建应用,选择模块

springboot帮我们配置了什么?能不能修改、能不能扩展、能改哪些东西

  • xxxxAutoConfiguration;向容器中自动配置组件
  • xxxxProperties:自动配置类,装配配置文件中自定义的一些内容

要解决的问题;

  • 导入静态资源…
  • 首页
  • jsp,模板引擎Thymelea
  • 装配扩展springMVC
  • 增删改查
  • 拦截器
  • 国际化

静态资源

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
        this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
            registration.addResourceLocations(this.resourceProperties.getStaticLocations());
            if (this.servletContext != null) {
                ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                registration.addResourceLocations(new Resource[]{resource});
            }

        });
    }
}

在这里插入图片描述

什么是wenjars

总结:

  1. 在springboot中我们可以使用一下方式访问静态资源
    1. webjars ——ip:port/webjars/***
    2. resource、public、static ——ip:port/***
  2. 优先级: resource 》 static 》public

在springboot的resource目录下:resource、public、static文件夹都可以放静态资源,

首页如何定制

模板引擎

结论:只要需要使用thymeleaf,只需要导入对应的依赖就可以了,我们将html放到我们的template目录下即可!

<!--        基于3.x版本开发-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值