18.Springboot(1)

Springboot一.介绍1.什么是Springboot随着 Spring不断的发展,涉及的领域越来越多,项目整合开发需要配合各种各样的文件,慢慢变得不那么易用简单,违背了最初的理念,甚至人称配置地狱。SpringBoot 正是在这样的一个背景下被抽象出来的开发框架,目的为了让大家更容易的使用 Spring 、更容易的集成各种常用的中间件、开源软件;Spring Boot 基于 Spring 开发,Spirng Boot 本身并不提供 Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地
摘要由CSDN通过智能技术生成

Springboot(1)

一.介绍

1.什么是Springboot
  • 随着 Spring不断的发展,涉及的领域越来越多,项目整合开发需要配合各种各样的文件,慢慢变得不那么易用简单,违背了最初的理念,甚至人称配置地狱。SpringBoot 正是在这样的一个背景下被抽象出来的开发框架,目的为了让大家更容易的使用 Spring 、更容易的集成各种常用的中间件、开源软件;
  • Spring Boot 基于 Spring 开发,Spirng Boot 本身并不提供 Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于 Spring 框架的应用程序。也就是说,它并不是用来替代 Spring的解决方案,而是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。**Spring Boot以约定大于配置的核心思想,默认帮我们进行了很多设置,多数 Spring Boot 应用只需要很少的 Spring配置。**同时它集成了大量常用的第三方库配置(例如 Redis、MongoDB、Jpa、RabbitMQ、Quartz 等等),SpringBoot 应用中这些第三方库几乎可以零配置的开箱即用。
  • 简单来说就是SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,springboot整合了所有的框架 。
2.SpringBoot优点
  • 创建独立Spring应用
  • 内嵌web服务器
  • 自动starter依赖,简化构建配置
  • 自动配置Spring以及第三方功能
  • 提供生产级别的监控、健康检查及外部化配置
  • 无代码生成、无需编写XML
3.微服务架构
  • 微服务是一种架构风格
  • 一个应用拆分为一组小型服务
  • 每个服务运行在自己的进程内,也就是可独立部署和升级
  • 服务之间使用轻量级HTTP交互
  • 服务围绕业务功能拆分
  • 可以由全自动部署机制独立部署
  • 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术

二.入门

1.项目创建方式一:使用Spring Initializr 的 Web页面创建项目
  1. 打开 https://start.spring.io/
  2. 填写项目信息
  3. 点击”Generate Project“按钮生成项目;下载此项目
  4. 解压项目包,并用IDEA以Maven项目导入,一路下一步即可,直到项目导入完毕。
  5. 如果是第一次使用,可能速度会比较慢,包比较多、需要耐心等待一切就绪。
2.项目创建方式二:使用 IDEA 直接创建项目(推荐)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
项目结构分析:
通过上面步骤完成了基础项目的创建。就会自动生成以下文件:
1、程序的主启动类
在这里插入图片描述
2、一个 application.properties 配置文件
在这里插入图片描述
3、一个 测试类
在这里插入图片描述
4、一个 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>

    <!-- 父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.liqingfeng</groupId>
    <artifactId>springboot-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
       <!-- web场景启动器 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <!-- springboot单元测试 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
   </dependencies>

   <build>
       <plugins>
           <!-- 打包插件 -->
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>
</project>

现在我们写一个controller业务
1、在主启动类的同级目录下新建一个controller包(必须是同级目录下,底层写好的),并新建一个HelloController类
请添加图片描述
2、从主程序类中启动项目(执行main方法),请求hello请求,页面显示返回的数据,OK
图片
将项目打成jar包,发布出去
1.打包之前,在pom.xml中配置打包时跳过test测试(节省打包时间,且jar包也不会太大)

<!--在工作中,很多情况下我们打包是不想执行测试用例的(测试影响数据库数据),所以跳过测试-->
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <configuration>
                   <!--跳过项目运行测试用例-->
                   <skipTests>true</skipTests>
               </configuration>
           </plugin>
       </plugins>

2.执行打包操作
在这里插入图片描述
3.打包成功会在target目录下看到对应jar包
在这里插入图片描述
4.将该jar包拖到桌面并执行cmd的java -jar命令执行jar包
在这里插入图片描述
5.浏览器请求url,访问OK
在这里插入图片描述

3.自定义banner启动图标

在resources目录下新建一个banner.txt,其内容可在https://www.bootschool.net/ascii生成
在这里插入图片描述

三.自动配置原理(重点)

我们之前写的springboot01 web项目,执行主启动类main方法就能运行服务。到底是怎么运行的呢?

1.从pom.xml文件探究起

在这里插入图片描述
点进去spring-boot-starter-parent,发现还有一个父依赖spring-boot-dependencies
在这里插入图片描述
这里才是真正管理SpringBoot应用里面所有依赖版本的地方,SpringBoot的版本控制中心;
以后我们导入依赖默认是不需要写版本;但是如果导入的包没有在依赖中管理着就需要手动配置版本了

我们pom.xml要开发web应用,如下配置了,就不用写版本号,因为依赖中版本已控制好,而且spring-boot-starter-web叫web场景启动器,该启动器会导入web开发中正常运行所依赖的jar包(其中包含嵌入的tomcat服务器)在这里插入图片描述

2.启动器

以后我们看到的springboot-boot-starter-xxx,就是spring-boot的各场景启动器
看到:xxx-spring-boot-starter 就是第三方为我们提供的,简化开发的场景启动器
官网启动器详解:https://docs.spring.io/spring-boot/docs/2.4.11/reference/html/using-spring-boot.html#using-boot-starter在这里插入图片描述
SpringBoot将所有的功能场景都抽取出来,做成一个个的starter (启动器),只需要在项目中引入这些starter即可,所有相关的依赖都会导入进来 , 我们要用什么功能就导入什么样的场景启动器即可,我们未来也可以自己自定义 starter

3.主启动类
@SpringBootApplication
public class Springboot01Application {
   

    public static void main(String[] args) {
   
        SpringApplication.run(Springboot01Application.class, args);
    }
}

@SpringBootApplication注解
作用:标注在某个类上说明这个类是SpringBoot的主配置类 , SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;
进入这个注解:可以看到上面还有很多其他注解!
在这里插入图片描述

@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("......")
1.@ComponentScan

作用:自动扫描并加载符合条件的bean组件到IOC容器中,spring中有讲解

2.@SpringBootConfiguration

我们继续进去这个@SpringBootConfiguration注解查看,该注解又是一个@Configuration
在这里插入图片描述
点@Configuration注解查看,@Configuration注解又是一个 @Component
在这里插入图片描述

3.@Configuration

功能:作用的类是一个配置类(配置类就是对应Spring的xml 配置文件)

  • Full模式(@Configuration(proxyBeanMethods = true)),是默认模式
  • Lite模式(@Configuration(proxyBeanMethods = false))

两种模式推荐最佳实战

  • 配置类的bean组件之间有依赖关系用Full模式(每个@Bean方法被调用多少次返回的bean组件都是单实例的(配置类里面使用@Bean标注在方法上给容器注册组件默认是单实例的),也就是这些bean组件地址是同一个,因为会去spring容器中找这个bean组件)
  • 配置类的bean组件之间无依赖关系用Lite模式(每个@Bean方法被调用多少次返回的bean组件都是新创建的,也就是这些bean组件地址不是同一个,因为会直接new一个bean组件返回,不会去spring容器中找。不过,可以加速容器启动过程、减少判断)
4.@Component

功能:作用的类,该类是spring容器里的bean组件。
所以说,主启动类本身也是Spring中的一个配置类也是一个bean组件,负责启动应用!

5.@EnableAutoConfiguration(重要的注解)

功能:开启自动配置功能
在这里插入图片描述

6.@AutoConfigurationPackage

@import : 给spring容器中导入Registrar bean组件
在这里插入图片描述
Registrar类作用:将主启动类的所在包及包下面所有子包里面的所有bean组件注册到Spring容器
下图通过debug追踪AutoConfigurationPackage.class.getName()就是扫描主启动类(Springboot01Application)包下的所有组件在这里插入图片描述
在这里插入图片描述

7.@Import
  • @Import( ):给spring容器导入指定的bean组件
  • @Import(AutoConfigurationImportSelector.class):给spring容器注册AutoConfigurationImportSelector bean组件,而AutoConfigurationImportSelector是自动配置导入选择器,它被spring容器托管,那么它会导入哪些自动配置类呢?

1、AutoConfigurationImportSelector类中有一个这样的方法

// 获得候选的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
   
    //这里的getSpringFactoriesLoaderFactoryClass()方法
    //返回的就是我们最开始看的启动自动导入配置文件的注解类;EnableAutoConfiguration
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
				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;
}

2、这个方法又调用了 SpringFactoriesLoader 类的静态方法!我们进入SpringFactoriesLoader类loadFactoryNames() 方法

public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
   
    String factoryClassName = factoryClass.getName();
    //这里它又调用了 loadSpringFactories 方法
    return (List)loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}

3、我们继续点击查看 loadSpringFactories 方法

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
   
    //获得classLoader , 我们返回可以看到这里得到的就是EnableAutoConfiguration标注的类本身
    Map<String, String> result = (Map)cache.get(classLoader);
    if (result != null) {
   
        return result;
    } else {
   
   		HashMap result = new HashMap();
        try {
   
            //去获取一个资源 "META-INF/spring.factories"
           Enumeration urls = classLoader.getResources("META-INF/spring.factories");

            //将读取到的资源遍历,封装成为一个Properties
            while(urls.hasMoreElements()) {
   
                URL url = (URL)urls.nextElement();
                UrlResource resource = new UrlResource(url);
                Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                Iterator var6 = properties.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:即错误信息为:nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cloud.lowcode.mapper.UserMapper.queryByParam。引用\[3\]:总结解决 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)方法 问题背景:在做SpringBoot项目的时候,通过controller层调用service层的接口时出现了如下的报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.service.UserService.getById at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~\[mybatis-3.5.2.jar:3.5.2\] at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:49) ~\[mybatis-plus-core-3.2.0.jar:3.2.0\] at com.example.controller.UserController.obj(UserController.java:18) ~\[classes/:na\] 1.问题原因是由于dao层(也可以叫做mapper接口)跟mapper.xml文件没有映射,而大部分的原因有如下的几种类型: dao层的方法跟mapper.xml的方法不一样,mapper.xml中的namespace要写对应的dao层和entity层不一样,spring配置文件中mybatis与xml文件路径的配置没有写,导致无法映射成功,拼写错误导致。\[3\]根据你提供的错误信息,可以推断出你的问题是在调用com.yk.springboot.service.UserService.findAllUser方法时出现了org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)的错误。这个错误通常是由于mapper接口与mapper.xml文件没有正确映射导致的。你可以检查以下几个方面来解决这个问题:1. 确保你的mapper接口中的方法名与mapper.xml文件中的statement id一致;2. 确保mapper.xml文件中的namespace与mapper接口的包路径一致;3. 检查你的spring配置文件中是否正确配置了mybatis与xml文件路径的映射关系;4. 检查你的代码中是否存在拼写错误。希望这些解决方法能够帮助你解决问题。 #### 引用[.reference_title] - *1* *2* [解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):xxx问题](https://blog.csdn.net/lvoelife/article/details/128017529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [总结mybatis plus解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)...](https://blog.csdn.net/weixin_44106947/article/details/122910406)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值