• SpringBoot学习笔记(1)——B站动力节点


    001- JavaConfig

    1.1 为什么使用springboot

    在这里插入图片描述

    1.2 JavaConfig

    JavaConfig: 使用java类作为xml配置文件的替代, 是配置spring容器的纯java的方式。 
    在这个java类就可以创建java对象,把对象放入spring容器中(注入到容器)
    
    使用两个注解:
    1)@Configuration: 放在一个类的上面,表示这个类是作为配置文件使用的。
    2)@Bean:声明对象,把对象注入到容器中。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建一个空项目 再创建一个maven java 模块进行演示
    
    • 1

    在这里插入图片描述


    使用原本的方法 xml配置文件
    
    • 1

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述


    使用javaconfig
    
    • 1

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述在这里插入图片描述

    1.3 @ImporResource

    作用:导入其他的xml配置文件,等于在xml中的
    
    • 1
    <import resources="其他配置文件"/>
    
    • 1

    在这里插入图片描述在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

    1.4 @PropertyResource

    @PropertyResource: 
    	读取properties属性配置文件。
    	使用属性配置文件可以实现外部化配置,在程序代码之外提供数据。
    
    步骤:
    	在resources目录下,创建properties文件, 使用k=v的格式提供数据
    	在@PropertyResource 指定properties文件的位置
    	使用@Value(value=“${key}”)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

    002- springboot 入门

    2.1 介绍

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

    特点:

    • 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 创建springboot项目

    使用Spring提供的初始化器,就是向导,创建SpringBoot应用
    
    默认使用的地址:Default:https://start.spring.io
    国内地址:Custom:https://start.springboot.io
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

    pom.xml文件:
    
    • 1
    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0modelVersion>
    
    	
    	<parent>
    		<groupId>org.springframework.bootgroupId>
    		<artifactId>spring-boot-starter-parentartifactId>
    		<version>2.4.2version>
    		<relativePath/>
    	parent>
    
    	
    	<groupId>com.bjpowernodegroupId>
    	<artifactId>002-springboot-firstartifactId>
    	<version>1.0.0version>
    
    
    	<properties>
    		<java.version>1.8java.version>
    	properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-webartifactId>
    		dependency>
    
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-testartifactId>
    			<scope>testscope>
    		dependency>
    	dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    			plugin>
    		plugins>
    	build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    在这里插入图片描述


    在这里插入图片描述


    2.3 入门项目

    使用springboot实现简单的控制层操作
    
    和上面一样 使用maven创建一个模块 spring initializr
    只选spring web 依赖
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    2.4 @SpringBootApplication

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

    在这里插入图片描述

    2.@EnableAutoConfiguration
    
    启用自动配置,把java对象配置好,注入到spring容器中。
    例如可以把mybatis的对象创建好,放入到容器中
    
    3.@ComponentScan
    
    @ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。
    默认扫描的包:有@ComponentScan注解的类所在的包和子包。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    所以其他代码一定得和Application文件平级或小于

    2.5 springboot重要配置文件

    即application.properties
    springboot的配置文件有两种格式 .properties 和.yml
    
    配置文件名称:application
    
    扩展名有:
    	properties	(k=v)
    	yml 		(k: v) (k:空格v)
    
    使用application.properties,application.yml
    .yml更轻量 主推这个
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

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

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

    例2: application.yml

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

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

    2.6 多环境配置

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

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

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

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

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

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

    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

    2.7 springboot自定义配置

    (1)@Value

    SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配置,
    然后采用如下注解去读取配置的属性值
    	@Value("${key}") , key 来自 application.properties(yml)
    
    • 1
    • 2
    • 3

    在这里插入图片描述在这里插入图片描述在这里插入图片描述

    (2)@ConfigurationProperties

    将整个文件映射成一个对象,用于自定义配置项比较多的情况
    
    • 1

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

    2.8 springboot使用 jsp

    不推荐在spingboot使用jsp
    springboot也不提供使用jsp 需要进行配置才能使用
    在springboot中使用jsp逐渐会被淘汰
    
    会使用模板来完成视图层的操作
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用jsp需要配置:

    1. 加入一个处理jsp的依赖。 负责编译jsp文件
    <dependency>
        <groupId>org.apache.tomcat.embedgroupId>
        <artifactId>tomcat-embed-jasperartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    1. 如果需要使用servlet, jsp,jstl的功能
    <dependency>
    	<groupId>javax.servletgroupId>
    	<artifactId>jstlartifactId>
    dependency>
    
    <dependency>
    	<groupId>javax.servletgroupId>
    	<artifactId>javax.servlet-apiartifactId>
    dependency>
    
    <dependency>
    <groupId>javax.servlet.jspgroupId>
    	<artifactId>javax.servlet.jsp-apiartifactId>
    	<version>2.3.1version>
    dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 创建一个存放jsp的目录,一般叫做webapp

      index.jsp

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

      META-INF/resources

    3. 创建Controller, 访问jsp

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


    具体操作见视频
    
    • 1

    在这里插入图片描述
    在这里插入图片描述

    2.9 springboot中使用ApplicationContext容器

    如果你想通过代码,从容器中获取对象。
    SpringApplication.run(Application.class, args)的返回值就是ApplicationContext容器。
    
    • 1
    • 2

    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

    2.10 ComnandLineRunner 接口 ApplcationRunner 接口

    开发中可能会有这样的情景。需要在容器启动后执行一些内容。
    比如读取配置文件,数据库连接之类的。
    SpringBoot 给我们提供了两个接口来帮助我们实现这种需求。
    这两个接口分别为 CommandLineRunner 和 ApplicationRunner。
    他们的执行时机为容器启动完成的时候。
    
    这两个接口都有一个 run 方法,我们只需要实现这个方法即可。
    这两个接口的不同之处在于: 
    	ApplicationRunner 中 run 方法的参数为 ApplicationArguments
    	而CommandLineRunner接口中 run 方法的参数为 String 数组
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    @FunctionalInterface
    public interface CommandLineRunner {
        void run(String... args) throws Exception;
    }
    
    @FunctionalInterface
    public interface ApplicationRunner {
        void run(ApplicationArguments args) throws Exception;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    Flutter 实现局部刷新 StreamBuilder 实例详解
    Windows11 WSL2 Ubuntu编译安装perf工具
    xbox下载游戏速度一直为0b/s怎么办?
    opengauss数据备份(docker中备份)
    泛微E8事务回滚类
    Spark shuffle
    王道书 P150 T20 + 拓展(表达式树的计算)
    大学生HTML作业节日网页 HTML作业节日文化网页期末作业 html+css+js节日网页 HTML学生节日介绍 HTML学生作业网页视频
    TCP协议之《预分配缓存额度sk_forward_alloc--TCP发送》
    【Python】快捷调用百度ocr能力
  • 原文地址:https://blog.csdn.net/m0_52041525/article/details/125867299