SpringBoot超详细讲解

秒速五厘米 2024-03-31 16:42 174阅读 0赞

SpringBoot

  • 第一章 JavaConfig
    • 1.1 JavaConfig
    • 1.2 @ImporResource
    • 1.3 @PropertyResource
  • 第二 章 Spring Boot
    • 2.1 介绍
    • 2.2 创建Spring Boot项目
      • 2.2.1 第一种方式, 使用Spring提供的初始化器, 就是向导创建SpringBoot应用
      • 2.2.1 使用国内的地址
    • 2.3 注解的使用
    • 2.4 SpringBoot的配置文件
    • 2.5 多环境配置
    • 2.6 @ConfigurationProperties
    • 2.7 使用jsp
    • 2.8 使用容器
    • 2.9 ComnandLineRunner 接口 , ApplcationRunner接口
  • 第三章 Web组件
    • 3.1 拦截器
    • 3.2 Servlet
    • 3.3 过滤器Filter
    • 3.4 字符集过滤器
  • 第四章 ORM 操作 MySQL
      • 第一种方式 : @Mapper
      • 第二种方式 @MapperScan
      • 第三种方式: Mapper文件和Dao接口分开管理
      • 第四个 事务
  • 第五章 接口架构风格 —RESTful
      • 5.1 REST
      • 5.2 在页面中或者ajax中,支持put,delete请求
  • 第六章 Redis
    • 6.1 配置Windows版本的redis
    • 6.2 对比 StringRedisTemplate 和 RedisTemplate
  • 第七章 SpringBoot集成Dubbo
    • 7.1 看 SpringBoot继承Dubbo的文档
    • 7.2 公共项目
    • 7.3 提供者
    • 7.4消费者
    • 7.5 练习
  • 第八章 打包
    • 8.1 打包war
    • 8.2 打包为jar
  • 第九章 Thymeleaf 模板引擎
    • 9.1 表达式
    • 9.2 Thymeleaf属性
    • 9.3 each
    • 9.4 th:if
    • 9.5 th:switch
    • 9.6 th:inline
    • 9.7 字面量
    • 9.8 字符串连接
    • 9.9 运算符
    • 9.10 内置对象
    • 9.11 内置工具类
    • 9.12 自定义模板
  • 第十章 总结
    • 10.1 注解

第一章 JavaConfig

  1. 为什么要使用 Spring Boot

    因为Spring, SpringMVC 需要使用的大量的配置文件 (xml文件)

    还需要配置各种对象,把使用的对象放入到spring容器中才能使用对象

    需要了解其他框架配置规则。

  2. SpringBoot 就相当于 不需要配置文件的Spring+SpringMVC。 常用的框架和第三方库都已经配置好了。

    拿来就可以使用了。

  3. SpringBoot开发效率高,使用方便多了

1.1 JavaConfig

JavaConfig: 使用java类作为xml配置文件的替代, 是配置spring容器的纯java的方式。 在这个java类这可以创建java对象,把对象放入spring容器中(注入到容器),

使用两个注解:

1)@Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的。

2)@Bean:声明对象,把对象注入到容器中。

  1. 例子:
  2. package com.bjpowernode.config;
  3. import com.bjpowernode.vo.Student;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * Configuration:表示当前类是作为配置文件使用的。 就是用来配置容器的
  8. * 位置:在类的上面
  9. *
  10. * SpringConfig这个类就相当于beans.xml
  11. */
  12. @Configuration
  13. public class SpringConfig {
  14. /**
  15. * 创建方法,方法的返回值是对象。 在方法的上面加入@Bean
  16. * 方法的返回值对象就注入到容器中。
  17. *
  18. * @Bean: 把对象注入到spring容器中。 作用相当于<bean>
  19. *
  20. * 位置:方法的上面
  21. *
  22. * 说明:@Bean,不指定对象的名称,默认是方法名是 id
  23. *
  24. */
  25. @Bean
  26. public Student createStudent(){
  27. Student s1 = new Student();
  28. s1.setName("张三");
  29. s1.setAge(26);
  30. s1.setSex("男");
  31. return s1;
  32. }
  33. /***
  34. * 指定对象在容器中的名称(指定<bean>的id属性)
  35. * @Bean的name属性,指定对象的名称(id)
  36. */
  37. @Bean(name = "lisiStudent")
  38. public Student makeStudent(){
  39. Student s2 = new Student();
  40. s2.setName("李四");
  41. s2.setAge(22);
  42. s2.setSex("男");
  43. return s2;
  44. }
  45. }

1.2 @ImporResource

@ImportResource 作用导入其他的xml配置文件, 等于 在xml

  1. <import resources="其他配置文件"/>

例如:

  1. @Configuration
  2. @ImportResource(value ={
  3. "classpath:applicationContext.xml","classpath:beans.xml"})
  4. public class SpringConfig {
  5. }

1.3 @PropertyResource

@PropertyResource: 读取properties属性配置文件。 使用属性配置文件可以实现外部化配置 ,

在程序代码之外提供数据。

步骤:

  1. 在resources目录下,创建properties文件, 使用k=v的格式提供数据
  2. 在PropertyResource 指定properties文件的位置
  3. 使用@Value(value=“${key}”)

    @Configuration
    @ImportResource(value ={

    1. "classpath:applicationContext.xml","classpath:beans.xml"})

    @PropertySource(value = “classpath:config.properties”)
    @ComponentScan(basePackages = “com.bjpowernode.vo”)
    public class SpringConfig {

  1. }

第二 章 Spring Boot

2.1 介绍

SpringBoot是Spring中的一个成员, 可以简化Spring,SpringMVC的使用。 他的核心还是IOC容器。

特点:

  • Create stand-alone Spring applications

    创建spring应用

  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

    内嵌的tomcat, jetty , Undertow

  • Provide opinionated ‘starter’ dependencies to simplify your build configuration

    提供了starter起步依赖,简化应用的配置。

    比如使用MyBatis框架 , 需要在Spring项目中,配置MyBatis的对象 SqlSessionFactory , Dao的代理对象

    在SpringBoot项目中,在pom.xml里面, 加入一个 mybatis-spring-boot-starter依赖

  • Automatically configure Spring and 3rd party libraries whenever possible

    尽可能去配置spring和第三方库。叫做自动配置(就是把spring中的,第三方库中的对象都创建好,放到容器中, 开发人员可以直接使用)

  • Provide production-ready features such as metrics, health checks, and externalized configuration

    提供了健康检查, 统计,外部化配置

  • Absolutely no code generation and no requirement for XML configuration

    不用生成代码, 不用使用xml,做配置

2.2 创建Spring Boot项目

2.2.1 第一种方式, 使用Spring提供的初始化器, 就是向导创建SpringBoot应用

使用的地址: https://start.spring.io

SpringBoot项目的结构:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vNL2pMLm-1670548266762)(D:\course\25-SpringBoot\笔记\images\image-20210115152427829.png)]

2.2.1 使用国内的地址

https://start.springboot.io

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lyHqDqmn-1670548266763)(D:\course\25-SpringBoot\笔记\images\image-20210115155556662.png)]

2.3 注解的使用

  1. @SpringBootApplication
  2. 符合注解:由
  3. @SpringBootConfiguration
  4. @EnableAutoConfiguration
  5. @ComponentScan
  6. 1.@SpringBootConfiguration
  7. @Configuration
  8. public @interface SpringBootConfiguration {
  9. @AliasFor(
  10. annotation = Configuration.class
  11. )
  12. boolean proxyBeanMethods() default true;
  13. }
  14. 说明:使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,
  15. 可以使用Bean声明对象,注入到容器

2.@EnableAutoConfiguration

启用自动配置, 把java对象配置好,注入到spring容器中。例如可以把mybatis的对象创建好,放入到容器中

3.@ComponentScan

  1. @ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。
  2. 默认扫描的包: @ComponentScan所在的类所在的包和子包。

2.4 SpringBoot的配置文件

配置文件名称: application

扩展名有: properties( k=v) ; yml ( k: v)

使用application.properties, application.yml

例1:application.properties设置 端口和上下文

  1. #设置端口号
  2. server.port=8082
  3. #设置访问应用上下文路径, contextpath
  4. server.servlet.context-path=/myboot

例2: application.yml

  1. server:
  2. port: 8083
  3. servlet:
  4. context-path: /myboot2

2.5 多环境配置

有开发环境, 测试环境, 上线的环境。

每个环境有不同的配置信息, 例如端口, 上下文件, 数据库url,用户名,密码等等

使用多环境配置文件,可以方便的切换不同的配置。

使用方式: 创建多个配置文件, 名称规则: application-环境名称.properties(yml)

创建开发环境的配置文件: application-dev.properties( application-dev.yml )

创建测试者使用的配置: application-test.properties

2.6 @ConfigurationProperties

@ConfigurationProperties: 把配置文件的数据映射为java对象。

属性:prefix 配置文件中的某些key的开头的内容。

  1. @Component
  2. @ConfigurationProperties(prefix = "school")
  3. public class SchoolInfo {
  4. private String name;
  5. private String website;
  6. private String address;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public String getWebsite() {
  14. return website;
  15. }
  16. public void setWebsite(String website) {
  17. this.website = website;
  18. }
  19. public String getAddress() {
  20. return address;
  21. }
  22. public void setAddress(String address) {
  23. this.address = address;
  24. }
  25. @Override
  26. public String toString() {
  27. return "SchoolInfo{" +
  28. "name='" + name + '\'' +
  29. ", website='" + website + '\'' +
  30. ", address='" + address + '\'' +
  31. '}';
  32. }
  33. }

application.properties

  1. #配置端口号
  2. server.port=8082
  3. #context-path
  4. server.servlet.context-path=/myboot
  5. #自定义key=value
  6. school.name=动力节点
  7. school.website=www.bjpowernode.com
  8. school.address=北京的大兴区
  9. site=www.bjpowernode.com

2.7 使用jsp

SpringBoot不推荐使用jsp ,而是使用模板技术代替jsp

使用jsp需要配置:

1) 加入一个处理jsp的依赖。 负责编译jsp文件

  1. <dependency>
  2. <groupId>org.apache.tomcat.embed</groupId>
  3. <artifactId>tomcat-embed-jasper</artifactId>
  4. </dependency>
  1. 如果需要使用servlet, jsp,jstl的功能


    javax.servlet
    jstl

    javax.servlet
    javax.servlet-api

    javax.servlet.jsp
    javax.servlet.jsp-api
    2.3.1
  2. 创建一个存放jsp的目录,一般叫做webapp

index.jsp

  1. 需要在pom.xml指定jsp文件编译后的存放目录。

META-INF/resources

5)创建Controller, 访问jsp

6)在application.propertis文件中配置视图解析器

2.8 使用容器

你想通过代码,从容器中获取对象。

通过SpringApplication.run(Application.class, args); 返回值获取容器。

  1. public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
  2. return run(new Class[]{
  3. primarySource}, args);
  4. }
  5. ConfigurableApplicationContext : 接口,是ApplicationContext的子接口
  6. public interface ConfigurableApplicationContext extends ApplicationContext

2.9 ComnandLineRunner 接口 , ApplcationRunner接口

这两个接口都 有一个run方法。 执行时间在容器对象创建好后, 自动执行run()方法。

可以完成自定义的在容器对象创建好的一些操作。

  1. @FunctionalInterface
  2. public interface CommandLineRunner {
  3. void run(String... args) throws Exception;
  4. }
  5. @FunctionalInterface
  6. public interface ApplicationRunner {
  7. void run(ApplicationArguments args) throws Exception;
  8. }

第三章 Web组件

讲三个内容: 拦截器, Servlet ,Filter

3.1 拦截器

拦截器是SpringMVC中一种对象,能拦截器对Controller的请求。

拦截器框架中有系统的拦截器, 还可以自定义拦截器。 实现对请求预先处理。

实现自定义拦截器:

  1. 创建类实现SpringMVC框架的HandlerInterceptor接口

    1. public interface HandlerInterceptor {
  1. default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  2. return true;
  3. }
  4. default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
  5. }
  6. default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
  7. }
  8. }

2.需在SpringMVC的配置文件中,声明拦截器

  1. <mvc:interceptors>
  2. <mvc:interceptor>
  3. <mvc:path="url" />
  4. <bean class="拦截器类全限定名称"/>
  5. </mvc:interceptor>
  6. </mvc:interceptors>

SpringBoot中注册拦截器:

  1. @Configuration
  2. public class MyAppConfig implements WebMvcConfigurer {
  3. //添加拦截器对象, 注入到容器中
  4. @Override
  5. public void addInterceptors(InterceptorRegistry registry) {
  6. //创建拦截器对象
  7. HandlerInterceptor interceptor = new LoginInterceptor();
  8. //指定拦截的请求uri地址
  9. String path []= {
  10. "/user/**"};
  11. //指定不拦截的地址
  12. String excludePath [] = {
  13. "/user/login"};
  14. registry.addInterceptor(interceptor)
  15. .addPathPatterns(path)

发表评论

表情:
评论列表 (有 0 条评论,174人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot详细讲解

    这篇文章主要介绍了SpringBoot通过自定义classloader加密class文件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考

    相关 RAID技术详细讲解

    RAID 技术是一种多磁盘技术,面对数据的各方面有着两面性的影响,整体来说优点大于缺点的,下面我将详细介绍一下 RAID ,简称磁盘阵列技术。 一、RAID 概述