SpringBoot学习笔记[持续更新中...]

SpringBoot

Spring Boot 2.6.1 https://spring.io/projects/spring-boot
Spring专栏https://blog.csdn.net/nrsc272420199/category_8739819.html

定位:是框架的框架,作用是简化常规框架的配置的方式,只需很少的代码即可以实现大量的功能,体现了开箱即用的思想

关于Maven依赖项版本的问题,原本使用框架时需要手动设置版本号,现SpringBoot框架引入后,所有的版本号信息,由SpringBoot官网测试并定义了版本号,只需要引入下面的这个依赖版本管理后,所需要的其他Jar包的版本信息就被导入了,以后导包的时候,如果在列表里面的,就不需要设置版本号了。

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>	
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

以下是完整的初始时SpringBoot项目的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <!-- maven坐标,必须唯一 -->
    <groupId>com.jt</groupId>
    <artifactId>springboot_demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_demo1</name>
    <description>Demo project for Spring Boot</description>

    <!-- 配置信息 -->
    <properties>
        <!--JDK版本-->
        <java.version>1.8</java.version>
        <!--构建字符集-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--输出字符集 -->
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--版本-->
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <!--指定各依赖项-->
    <dependencies>
        <!--原本使用框架时需要手动设置版本号,现SpringBoot框架引入后,所有的版本号信息,由SpringBoot官网测试并定义了版本号-->
        <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>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
	<!-- 引入官网测试好的,各常见Jar包的对应的版本信息 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
	
	<!--Maven SpringBoot的工具-->
    <build>
    	<!--插件s -->
        <plugins>
        	<!--Maven的生命周期管理工具 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <!--项目编译工具-->
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
            	<!--SpringBoot项目和常规项目不太一样,需引入此插件来进行打包-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.jt.SpringbootDemo1Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

视频作业:

12.10日 前需要看完的视频

1.脚手架的安装 https://www.bilibili.com/video/BV1SU4y1V7Jc?spm_id_from=333.999.0.0
2.1Vue组件路由1 https://www.bilibili.com/video/BV1bh411h7pZ?spm_id_from=333.999.0.0
2.2Vue组件路由2 https://www.bilibili.com/video/BV1Dw411o7ZZ?spm_id_from=333.999.0.0
3.SpringMVC https://www.bilibili.com/video/BV1qv411H7HR?spm_id_from=333.999.0.0
4.SpringBoot开箱即用原理 https://www.bilibili.com/video/BV1oh41167Aa?spm_id_from=333.999.0.0

开箱即用

SpringBoot将繁琐的配置封装到某些 jar包中,该暴打包中的文件已经完成了配置,引入即可使用,只需要少量的配置就可以获取其功能的方式,叫做开箱即用。

启动项:spring-boot-starter-xxxx
说明:包中已经将框架进行了整合,用户拿来就用

【Spring注解】@ComponentScan之includeFilters和excludeFilters https://blog.csdn.net/nrsc272420199/article/details/88385574

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootDemo1Application {
    /**
     * 主启动类执行时,开始加载@SpringBootApplication注解
     * @param args
     */
    public static void main(String[] args) {
        //SpringBoot的方式管理的Spring容器
        SpringApplication.run(SpringbootDemo1Application.class, args);
    }

}

以下是部分@SpringBootApplication注解的定义

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
//用来加载SpringBoot-starter-xxx的启动项,当主启动类执行时,则会开始加始加载启动项中的配置,则功能加载成功。
@EnableAutoConfiguration
// 定义包扫描路径为当前类所在的目录,如此即可扫描其下的所有子孙包
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}

yml文件

application.yml文件,将src/main/resources/application.properties文件的后缀直接修改为.yml

YML简介 https://www.jianshu.com/p/cea930923f3d

语法
  • k: v 表示键值对关系,冒号后面必须有一个空格

  • 使用空格的缩进表示层级关系,空格数目不重要,只要是左对齐的一列数据,都是同一个层级的

  • 大小写敏感

  • 缩进时不允许使用Tab键,只允许使用空格。

  • 松散表示,java中对于驼峰命名法,可用原名或使用-代替驼峰,如java中的lastName属性,在yml中使用lastName或 last-name都可正确映射。

lombok

pom.xml中配置依赖

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

然后在File -> settings -> Plugins里面搜索Lombok,看到如下的一个小辣椒的图标,如果没有安装就装一下,并根据提示重启IDEA
搜不到的,也可以到https://plugins.jetbrains.com/这个网站上去搜
在这里插入图片描述

常用注解

注解作用
@Data注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter注解在属性上;为属性提供 setting 方法
@Getter注解在属性上;为属性提供 getting 方法
@Log4j注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor注解在类上;为类提供一个全参的构造方法
@Accessors(chain = true)注解在类上;让类的setter方法返回当前对象本身,实现链式调用

编译后,对应的setter和getter方法就已经有了,所以打出的jar包放到服务器运行的时候就不需要再安装lombok的插件了

@SpringBootTest

规则说明:

  1. 运行被@Test标识的方法时,SpringBoot程序启动
  2. SpringBoot启动后,内部Spring容器也被启动,基于IOC管理的对象就可以使用DI注入对象了
  3. 可以在任意的测试类中获取想要的对象
  4. 基于以上,请保持测试方法所在类的路径与SpringBoot主启动类的包路径一致
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水晶心泉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值