目录
3) 添加spring boot父工程、spring依赖,web场景开发启动器
@AutoConfigurationPackage ,自动配置包
一、项目搭建、入门案例
1、方式1
1)创建一个空的maven工程
2)配置maven
3) 添加spring boot父工程、spring依赖,web场景开发启动器
<groupId>org.example</groupId>
<artifactId>springboot01</artifactId>
<version>1.0-SNAPSHOT</version>
<!--添加spring boot父工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.7.10</version>
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--web场景开发启动器,整合spring和springmvc包各种包 选中spring-boot-starter-web快捷键ctrl+shift+alt+u-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3)创建启动类
新建启动类
Spring Boot的启动类的作用是启动Spring Boot项目,是基于Main方法来运行的。
启动类在启动时会做注解扫描(@Controller、@Service、@Repository......),扫描位置为同包或者子包下的注解,所以启动类的位置应放于包的根下
启动类与启动器区别:
启动类表示项目的启动入口
启动器表示jar包的坐标
必须在包中新建这个类,不能直接放入到java文件夹。 在com.buba下新建自定义名称的类(规范:XXXXApplication),可以是项目上下文路径Application
package com.yka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName:ApplicationDemo
* @Auther: YooAo
* @Description: 主程序类 启动类
* @Date: 2023/3/31 08:49
* @Version: v1.0
*/
@SpringBootApplication
public class ApplicationDemo {
public static void main(String[] args) {
SpringApplication.run(ApplicationDemo.class,args);
}
}
4、启动
5、查看依赖传递
6、开发一个Controller
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/getTest")
public String getTest(){
return "test~~~~~~~";
}
}
端口号默认8080 项目上下文路径默认 ‘’ 其实就是没有
访问测试
2、方式2
在工作中可能会出现必须继承某个项目,如果Spring Boot用了继承就不能继承别的项目了。所以Spring Boot还提供了依赖的方式。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<!--在工作中可能会出现必须继承某个项目,如果Spring Boot用了继承就不能继承别的项目了。所以Spring Boot还提供了依赖的方式。-->
<dependency>
<!--spring-boot-dependencies就相当于使用spring-boot-starter-parent父依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--web场景开发启动器,整合spring和springmvc包各种包 选中spring-boot-starter-web快捷键ctrl+shift+alt+u-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3、方式3
使用idea自带springBoot项目初始化插件
二、启动原理
1、依赖导入原理
父项目版本控制
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
ctrl+ 点击spring-boot-dependencies 进入 这里管理着springboot中所有依赖的版本,版本控制中心,导入依赖如果不写版本就用这里的版本
spring-boot-starter-web,也就是web启动器,导入和很多web的依赖
2、springboot 包扫描原理
ctrl点击进入@SpringBootApplication注解
@SpringBootConfiguration
@SpringBootConfiguration 上面有一个Configuration表示这是一个配置类
配置类上面有@Component说明也是容器中的一个组件
@EnableAutoConfiguration
@EnableAutoConfiguration 启用自动配置功能 springboot中没有做任何配置,springboot自动帮助我们配置,但是要通过该注解才能生效 @AutoConfigurationPackage // 自动扫描配置类包的注解 @Import(AutoConfigurationImportSelector.class)// 给容器导入一些组件的选择器,导入一些默认配置
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@AutoConfigurationPackage ,自动配置包
@Import(AutoConfigurationPackages.Registrar.class) 导入AutoConfigurationPackages.Registrar类 @Import是spring的 底层注解,给容器导入一个组件
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
}