每日学习Java之一万个为什么

发布于:2025-03-18 ⋅ 阅读:(319) ⋅ 点赞:(0)

场景启动器:starter

参考常见启动器

默认配置

官网默认值

依赖

见官网 / pom父依赖

注解

  • @SpringBootApplication:启动自动装配,配合 main + SpringApplication.run(.class,args)

  • @SpringBootTest:SpringBoot单元测试注解

  • @ConfigurationProperties:指定前缀读取配置文件对应属性,实现批量读取属性值

  • @Import:导入其他配置类 / 组件

    • 1.硬编码一个数组,汇总导入
    • 2.配合importSelector接口实现类,IO读取外部配置n个组件,然后@Import实现类
  • @Conditional:条件化注入+@Bean 自己从来不用,第三方框架使用

  • @MapperScan:Mybatis提供的Spring扫描注解

SpringApplication.run会干什么

创建IOC容器,初始化

SpringBoot三大优势

  • 简化整合
  • 简化开发
  • 简化配置

Maven插件爆红

logback加载失败位于本地仓库ch/qos目录下

解决办法,清除文件重新加载

boot 父依赖版本设置

SpringBoot Maven Plugin

SpringBoot 整合 MVC

WebMvcConfigurer 配置类 注册自定义拦截器配置拦截地址

SpringBoot 整合 Mybatis

  • 依赖:原先mybatis依赖替换成场景启动器

  • config:yml配置 configuration typeAlias

  • mapper:MapperScan 扫描sql映射文件

  • 接口文件:yml扫描接口

  • 连接:spring配置连接池

  • 调用:service配置mapper属性注入接口名,调用sql方法映射

SpringBoot整合AOP TX

导入依赖即可自动装配
声明式事务相关依赖为 jdbc二非tx

META-INF 包代表什么 SpringBoot三个静态资源包名称

META-INF 目录:主要用于存放 Spring 配置文件、Maven 插件描述符、SPI 服务提供者列表以及其他元数据文件。

public, resources, META-INF/resources , static

SpringBoot整合日志

参考

SpringBoot启动类和其他类有什么不同

启动类以main方法作为项目入口 + @SpringBootApplication注解

什么时候创建IOC容器

SpringApplication.run 启动IOC容器

自定义类必须放在启动类包下吗?

可以手动指定。不过@SpringBootApplication 中的@Component 是默认值,仅扫描所在包及其子包。不过其实按照约定放置Main方法位置就可以。

SpringBoot自动装配

  • 准备好的配置类
  • 固定文件写了配置类清单 文件路径
  • 启动类 @Import + ImportSelector 完成批量导入
  • @Conditional 注解 启动导入的场景(条件化注入)

如何将非启动类下的类加入SpringBootIOC容器

  • 1.将自己的类在启动类中通过@Bean 方法注入
  • 2.在启动类上方通过@Import 注解 导入目标类
  • 3.在@SpringBootApplication中扫描指定包

ImportSelector接口

参考

ImportSelector是SpringBoot中功能性注解类似Enable…格式的核心接口,Enable注解会启动头上的Import,召唤一个神器就是ImportSelector实现类,并对相关配置类进行扫描装配。
类似的还有ImportBeanDefinitionRegister

SpringBoot142个配置类清单路径

YOUR_REPOSITROY\org\springframework\boot\spring-boot-autoconfigure\3.0.5\spring-boot-autoconfigure-3.0.5.jar!\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports

为什么场景启动器不能随便导入,例如Mybatis必须启动数据源

  • 1.导入了场景启动器,就代表导入了数据源类
  • 2.触发数据源自动配置启动加入IOC
  • 3.url driver-class-name等参数为空报错

如果你不想要导入的类被启动,可以在@SpringBootApplication中手动排除

可变参数 … 及其应用场景

概念:可变参数(varargs)是一种在方法声明中允许传递任意数量参数的机制。在 Java 中,可变参数通过在参数类型后面添加三个点 (…) 来表示。这种特性使得方法可以更加灵活地处理不同数量的输入参数

Demo:

import java.util.ArrayList;
import java.util.List;

public class VarargsExample {

    // 打印多个数字
    public static void printNumbers(int... numbers) {
        System.out.print("Numbers: ");
        for (int number : numbers) {
            System.out.print(number + " ");
        }
        System.out.println();
    }

    // 创建并返回一个列表
    public static List<String> createList(String... items) {
        List<String> list = new ArrayList<>();
        for (String item : items) {
            list.add(item);
        }
        return list;
    }

    // 格式化字符串
    public static String formatString(String template, Object... args) {
        return String.format(template, args);
    }

    // 主方法
    public static void main(String[] args) {
        // 打印数字
        printNumbers(1, 2, 3, 4, 5);

        // 创建列表
        List<String> fruits = createList("apple", "banana", "cherry");
        System.out.println("Fruits: " + fruits);

        // 格式化字符串
        String greeting = formatString("Hello, %s! Today is %s.", "Alice", "Monday");
        System.out.println(greeting);
    }
}

Mybatis命名空间指定了对应的接口为什么还要用@Mapper或者@MapperScan

为了让Mapper代理对象装配到IOC容器中,使得其他业务可以直接调用代理实例。

Spring Boot 会使用 MapperFactoryBean 来创建 MyBatis 的 Mapper 代理对象,并将其作为 Spring Bean 进行管理

Git为什么需要暂存区

暂存区没有版本概念,用来缓存未完成的版本工作

  • 修改文件:echo “” >> < file>
  • 查看更改: git status
  • 提交暂存: git add . 或 < file> 支持模糊匹配
  • 显示差异: git diff
  • 取消暂存:git reset HEAD
  • 准备msg:git commit -m

git add . 时关于报错 LF will be replaced by CRLF

windows系统换行符问题,可以不用管

IDEA中add暂存了target直接回滚后会删除所有文件

感觉是IDEA GIT插件的问题,如果你没有退出IDEA,可以右键项目找到更改历史取消。
如果你退出重进就没了。
所以还是确认没有问题就Commit吧

.git 文件解密

参考


网站公告

今日签到

点亮在社区的每一天
去签到